划分
按照流向来进行划分
- 输入流 InputStream
- 输出流 OutputStream
按照类型来进行划分
- 字节流 InputStream 输入 OutputStream 输出
- 字符流 Reader 读 Writer写
OutputStream输出流
OutputStream 是一个抽象类,不能实例化,只能实例化其子类
常用方法
- public void close()throws IOException 关闭流资源
- public void flush()throws IOException 刷新缓冲区 也就是将缓冲区的数据 刷新到磁盘中
- public void write(byte[] b)throws IOException 将字节数组写入到磁盘中
- public void write(byte[] b,int off,int len) 指定字节数组的长度进行写入
close() 与flush()区别
- close() 底层调用了flush() 方法
- close() 表示关闭流对象资源 关闭这个流对象资源之后是不能使用
- flush() 将缓冲区的数据刷新到磁盘中(字符),流对象没有关闭,还是可以使用的
FileOutputStream
构造方法
- FileOutputStream(File file)将数据通过FileOutputStream 写入到文件
- FileOutputStream(String name)将数据通过FileOutputStream写入到指定的文件字符串路径中
- FileOutputStream(File file,boolean append)将数据通过FileOutputStream 写入到文件 参数 append - 如果为 true,则将字节写入文件末尾处,而不是写入文件开始处
package io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamTest { public static void main(String[] args) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(new File("a.txt"),false); FileOutputStream fileOutputStream1 = new FileOutputStream("b.txt"); byte[] bytes = "今天是星期六".getBytes(); byte[] bytes1 = "明天是星期天".getBytes(); fileOutputStream.write(bytes); fileOutputStream.write("\r\n".getBytes());//换行 fileOutputStream.write(bytes1); } }
结果
InputStream输入流
常用方法
- public void close()throws IOException 关闭流资源
- int read()一个一个字节进行读取 末尾取值-1
- int read(byte[] b)throws IOException 读取多个字节放入到数组中 返回值是读取有效的字节数
- int read(byte[] b,int off,int len)读取指定长度的字节放入到数组
package io; import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamTest { public static void main(String[] args) throws IOException { FileInputStream fileInputStream = new FileInputStream("a.txt"); byte[] bytes = new byte[1024]; while (fileInputStream.read(bytes)!=-1){ System.out.println(bytes.toString()); System.out.println(new String(bytes)); } fileInputStream.close(); } }
结果
高效流(BufferInputStream BufferOutputStream)
两个流本身没有读写文件的功能 只是对FileIntputStream 与FileOutputStream 进行封装
读取原理:也就是将文件中的数据一次性读取到字节数组中,读取8192个字节,下一次再读取的数据是不是从磁盘中获取数据,而是从缓冲区数组中获取数据,如果数组的字节(8192) 都读取完了之后,会从磁盘中再次读取8192个保存字节数组
高效的原因: 避免反复去向磁盘中读取的数据,而直接从内存中的字节数组中读取package io; import java.io.*; public class FileInputStreamTest { public static void main(String[] args) throws IOException { BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("c.txt")); bufferedOutputStream.write("今天是个好日子".getBytes()); bufferedOutputStream.flush(); BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("c.txt")); byte[] bytes = new byte[8192]; while(bufferedInputStream.read(bytes)!=-1){ System.out.println(new String(bytes)); } bufferedInputStream.close(); bufferedOutputStream.close(); } }
对象流
package io; import java.io.*; public class FileInputStreamTest { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d.txt",false)); objectOutputStream.writeObject(new Student("张三",20)); objectOutputStream.close(); ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("d.txt")); Object o = objectInputStream.readObject(); System.out.println(o); } public static class Student implements Serializable{ private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } }
字符流
package io; import java.io.*; public class FileInputStreamTest { public static void main(String[] args) throws IOException, ClassNotFoundException { FileWriter fileWriter = new FileWriter("e.txt"); fileWriter.write(97); fileWriter.write("好日子"); fileWriter.write(new char[]{'a','b'}); fileWriter.close(); FileReader fileReader = new FileReader("e.txt"); char[] chars = new char[1024]; int leng=-1; while((leng=fileReader.read(chars))!=-1){ System.out.println(leng); System.out.println(new String(chars,0,leng)); } } }
结果
转换流
- InputStreamReader 是字节流通向字符流的桥梁
- 可以指定其编码格式 使读写文件不出现乱码
- 为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader
- 构造方法 public InputStreamReader(InputStream in,Charset cs) 第一个参数字节输入流 第二参数是设置其编码格式
package io; import java.io.*; public class FileInputStreamTest { public static void main(String[] args) throws IOException { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("c.txt"),"UTF-8"); outputStreamWriter.write("今天是个好日子"); outputStreamWriter.close(); InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("c.txt"),"UTF-8"); char[] chars = new char[1024]; int leng=-1; while((leng=inputStreamReader.read(chars))!=-1){ System.out.println(leng); System.out.println(new String(chars,0,leng)); } inputStreamReader.close(); } }
字符缓冲区流(高效)
package io; import java.io.*; public class FileInputStreamTest { public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("f.txt"))); bufferedWriter.write("今天是个好日子"); bufferedWriter.newLine();//换行 bufferedWriter.write("明天也是个好日子"); bufferedWriter.close(); BufferedReader bufferedReader = new BufferedReader(new FileReader("f.txt")); String s=null; while ((s=bufferedReader.readLine())!=null){ System.out.println(s); } bufferedReader.close(); } }