IO框架
1. 字节缓冲流
提高IO效率,减少访问磁盘的次数
(1)BufferedInputStream 字节缓冲输入流
a. 创建BufferedInputStream(缓冲流的目的是为了增强字节流)
FileInputStream fileInputStream = new FileInputStream("e:\\aaa.txt"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
b. 读取
int data = 0; while((data = bufferedInputStream.read())!=-1){ System.out.print((char)data); } byte[] bytes = new byte[1024]; int count = 0; while ((count=bufferedInputStream.read(bytes))!=-1){ System.out.println(new String(bytes,0,count)); }
c. 关闭
bufferedInputStream.close();
(2)BufferedInputStream 字节缓冲输出流
a. 创建BufferedInputStream(缓冲流的目的是为了增强字节流)
FileOutputStream fileOutputStream = new FileOutputStream("e:\\buffer.txt"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
b. 读取
bufferedOutputStream.write("0123456".getBytes()); for (int i = 0; i < 10; i++) { bufferedOutputStream.write("helloworld".getBytes()); bufferedOutputStream.flush();//刷新到内存 }
c. 关闭
bufferedOutputStream.close();
2. 对象流
增强了缓冲区功能,增强了读写8种基本数据类型和字符串功能,增强了读写对象的功能:readObject()读取一个对象,writeObject(Object object) 向流中写入一个对象。
使用流传输对象的过程称为:序列化(写入),反序列化(读取)。
(1)ObjectOutputStream 实现对象的序列化,要求:序列化类必须实现Serializable接口
A.创建对象流
FileOutputStream fileOutputStream = new FileOutputStream("e:\\Student.bin"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
B. 序列化(写入操作)
public class Student implements Serializable { //实现Serializable接口,否则序列化不了 private String name; private int age; //若属性添加了transient(瞬间的)之后,这个属性就不能再序列化了 //静态属性也不能序列化
Student student = new Student("张三",12); objectOutputStream.writeObject(student); //序列化多个对象,可以借助集合实现 ArrayList<Student> list = new ArrayList<>(); list.add(student1); list.add(student2); objectOutputStream.writeObject(list);
C. 关闭
objectOutputStream.close();
(2)ObjectInputStream 实现对象的反序列化(读取重构成对象)
FileInputStream fileInputStream = new FileInputStream("e:\\Student.bin"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Student s = (Student) objectInputStream.readObject(); objectInputStream.close(); System.out.println(s.toString());
3. 字符流(读取中文可使用)
父类:Reader:字符输入流 Writer:字符输出流
(1)FileReader 读取字符文件
FileRear fr = new FileReader("e:\\hello.txt"); //创建文件字符输入流
读取和字节文件读取一致。
(2)FileWriter 写入文件
FileWriter fw = new FileWriter("d:\\write.txt");//创建文件字符输出流
写入和字节文件写入一致。
(3)使用FileReader和FileWriter复制文本文件,不能复制图片或二进制文件,使用字节流可复制任何文件。
4.字符缓冲流
(1)BufferedReader
FileRader fileRader = new FileRader("e:\\buffer.txt"); BufferedReader bufferedreader = new BufferedReader(fileRader); //一行一行的读取 String line = null; while((line = bufferedreader.readLine())!= null){ System.out.print(line); }
(2)BufferedWriter
FileWriter fileWriter = new FileWriter("e:\\buffer.txt"); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); //写入 for(int i = 0;i<10;i++){ bufferedWriter.writer("好好学习,天天向上!"); bufferedWriter.newLine();//写入一个换行符 \r\t bufferedWriter.flush(); } bufferedWriter.close();
5. PrintWriter
PrintWriter pw = new PrintWriter("d:\\print.txt");
pw.println(trew);
pw.close();