I/O是对于操作系统来说的,
-
输入流(InputStream)用于从外部源(如:键盘,文件,网络等)读取数据到程序中
-
输出流(outputStream)用于将程序中的数据写入到外部目标(如:文件,网络,打印机等)
字节流:一次读一个字节,一个字节就是一个byte大小
字符流:一次读一个字符,一个字符就是一个char大小
‘s’ 是一个字符,一个汉字 ‘好’ 也是一个字符,但是汉字一般是由两个字节组成,英文字符,是一个字节组成
一个字节是8 bit位
文件字节流---FileInputStream/FileOutputStream
FileInputStream文件输入流
public static void main(String[] args) {
FileInputStream inputStream = null; //定义可以先放在try外部
try {
inputStream = new FileInputStream("路径");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try { //建议在finally中进行,因为关闭流是任何情况都必须要执行的!
if(inputStream != null) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
因为使用流之后必须要关闭流,来完成对资源的释放,否则一直会被占用
但此写法太过繁杂
只要 该类实现了AutoCloseable接口就可以用try-with-resource语法,可以自动进行资源的释放
public static void main(String[] args) {
//注意,这种语法只支持实现了AutoCloseable接口的类!
try(FileInputStream inputStream = new FileInputStream("路径")) { //直接在try()中定义要在完成之后释放的资源
//.....
} catch (IOException e) { //这里变成IOException是因为调用close()可能会出现,而FileNotFoundException是继承自IOException的
e.printStackTrace();
}
//无需再编写finally语句块,因为在最后自动帮我们调用了close()
}
使用 read() 可以读取一个字节的数据,如果将流中的数据读完后,再读取就会返回-1
public static void main(String[] args) {
//test.txt:abcd
try(FileInputStream inputStream = new FileInputStream("test.txt")) {
int tmp;
while ((tmp = inputStream.read()) != -1){ //通过while循环来一次性读完内容
System.out.println((char)tmp);
}
}catch (IOException e){
e.printStackTrace();
}
}
available() 查看当前可读的剩余字节数量(并不是真实数据量,尤其再网络Io中,只是预估值)
try (FileInputStream inputStream = new FileInputStream("a.txt")){
//a.txt:abcd
System.out.println((char)inputStream.read());//a
System.out.println(inputStream.available());//3
}catch (IOException e){
e.printStackTrace();
}
一次性读取
public static void main(String[] args) {
//test.txt:abcd
try(FileInputStream inputStream = new FileInputStream("test.txt")) {
byte[] bytes = new byte[inputStream.available()]; //我们可以提前准备好合适容量的byte数组来存放
System.out.println(inputStream.read(bytes)); //一次性读取全部内容(返回值是读取的字节数)
System.out.println(new String(bytes)); //通过String(byte[])构造方法得到字符串
}catch (IOException e){
e.printStackTrace();
}
}
System.out.println(inputStream.read(bytes, 1, 2)); //第二个参数是从给定数组的哪个位置开始放入内容,第三个参数是读取流中的字节数
skip() 可以跳过指定数量的字节
FileOutputStream文件输出流
public static void main(String[] args) {
try(FileOutputStream outputStream = new FileOutputStream("output.txt")) {
outputStream.write('c'); //同read一样,可以直接写入内容
outputStream.write("lbwnb".getBytes()); //也可以直接写入byte[]
outputStream.write("lbwnb".getBytes(), 0, 1); //同上输入流
outputStream.flush(); //建议在最后执行一次刷新操作(强制写入)来保证数据正确写入到硬盘文件中
}catch (IOException e){
e.printStackTrace();
}
}
FileOutputStream outputStream = new FileOutputStream("output.txt",true);//append=true 开启追加模式
public static void main(String[] args) {
try(FileOutputStream outputStream = new FileOutputStream("output.txt");
FileInputStream inputStream = new FileInputStream("test.txt")) { //可以写入多个
byte[] bytes = new byte[10]; //使用长度为10的byte[]做传输媒介
int tmp; //存储本地读取字节数
while ((tmp = inputStream.read(bytes)) != -1){ //直到读取完成为止
outputStream.write(bytes, 0, tmp); //写入对应长度的数据到输出流
}
}catch (IOException e){
e.printStackTrace();
}
}
文件字符流---FileReader/FileWrite
public static void main(String[] args) {
try(FileReader reader = new FileReader("test.txt")){
reader.skip(1); //现在跳过的是一个字符
System.out.println((char) reader.read()); //现在是按字符进行读取,而不是字节,因此可以直接读取到中文字符
}catch (IOException e){
e.printStackTrace();
}
}
FileWriter writer = new FileWriter("output.txt")
writer.write('牛');
writer.append('牛'); //其实功能和write一样 只是能用链式调用
缓冲流
普通文件流读取数据都需要去外部IO设备区获取数据,就是从磁盘中读取到,数据一个字节/字符的 在IO总线中,以流的形式读取到应用程序,这样速度很慢
可以利用缓冲流,将数据先读到缓冲区(内存中),就可以一次性读取到程序中,从内存中读更快,并且再次需要读缓冲区中存在的数据,只需要从缓冲区拿,不去要再区请求外部IO设备
写入缓冲流同理
缓冲字节流
-
BufferedInputStream
-
BufferedOutputStream
public static void main(String[] args) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("test.txt"))){ //传入FileInputStream
System.out.println((char) bufferedInputStream.read()); //操作和原来的流是一样的
}catch (IOException e){
e.printStackTrace();
}
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(里面放一个字节流)
实际上 进行IO操作的不是BufferedInputStream(缓冲字节流)而是传入的FileInputStream(字节流)
运用装饰着模式 对FileInputStream中的方法进行额外的处理,然后在调用其同名方法
BufferedInputStream中的close方法
public void close() throws IOException {
byte[] buffer; //都会维护一个缓冲数组
while ( (buffer = buf) != null) {
if (bufUpdater.compareAndSet(this, buffer, null)) { //CAS无锁算法,并发会用到,暂时不需要了解
InputStream input = in;
in = null;
if (input != null)
input.close();//这里 调用的还是 inputStream的close方法
return;
}
// Else retry in case a new buf was CASed in fill()
}
}
reset() 和 mark()
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("test.txt"))
bufferedInputStream.mark();// 保留读到此处,
//mark(int readlimit)保留之后读取的readlimit数量的内容
//reset() 回到之前读取时候 的 mark()调用时候的位置
缓冲字符流
-
BufferedReader
-
BufferedWriter
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("test.txt"))){
System.out.println(reader.readLine()); //按行读取
}catch (IOException e) {
e.printStackTrace();
}
}
有 reader.readLine()
按行读取 返回的是字符串 并且可以进行流式调用
reader
.lines()
.limit(2)
.distinct()
.sorted()
.forEach(System.out::println);
同样也有 mark()
和 reset()
操作
转换流
可以将 字节流 转换成字符流 进行操作
//只有字节流 ,用转换流转换 就可以用 字符流的方式写入
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("test.txt"));
writer.write("lbwnb"); //以操作Writer的样子写入OutputStream
InputStreamReader reader = new InputStreamReader(new FileInputStream("test.txt"))
System.out.println((char) reader.read());
InputStreamReader和OutputStreamWriter本质也是Reader和Writer
是字符流
所以也可以放入缓冲流 BufferedReader中来实现
打印流
**PrintStream**
System.out
就是 打印流 PrintSteam
,是默认向控制台打印
PrintSteam
也是继承FilterOutputStream
类,依然是装饰器模式
但PrintSteam
不会抛出异常,所以你用System.out
的时候不用try-catch
其内部会处理这个异常,如果想要知道着异常是怎么回事,就可以调用checkError()
方法
PrintStream
可以格式化任意的类型,将它们以字符串的形式写入到输出流。
public static void main(String[] args) {
try(PrintStream stream = new PrintStream(new FileOutputStream("test.txt"))){
stream.println("lbwnb"); //其实System.out就是一个PrintStream
}catch (IOException e){
e.printStackTrace();
}
}
//只需要 一个字节输出流
可以看到,其中间会进行套娃,将 字节流 转换成 字符流 再装饰成 缓冲流 再装饰成 打印流
而执行的时候, 打印流将 应用程序的 字符串转换成字符放到缓冲区中(内存中),再将字符进行编码,转换成字节,利用字节输出流,输写入到文件
对象流
public static void main(String[] args) {
try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("output.txt"));
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("output.txt"))){
People people = new People("lbw");
outputStream.writeObject(people);
outputStream.flush();
people = (People) inputStream.readObject();
System.out.println(people.name);
}catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
static class People implements Serializable{ //必须实现Serializable接口才能被序列化
String name;
public People(String name){
this.name = name;
}
}
要传输的对象 要实现Serializable接口才能被序列化!!!这里就有了为什么实体类要实现Serializable接口的原因了
为了区别类的不同,在序列化时,会被自动添加这个属性,它代表当前类的版本,我们也可以手动指定版本。
transient关键字可以让类的属性 避免序列化