流的概念和作用
流是一组有顺序的、有起点和终点的字节集合,是对数据传输的总称和抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
IO流的分类
根据数据类型不同分为:字符流和字节流
根据数据流向不同分为:输入流和输出流
根据使用方式不同分为:节点流和处理流
- 字符流和字节流
字节是数据最小的基本单位,字节流就是对字节进行操作的流对象。多个字节可以组成字符,所以字符流的本质是基于字节流读取时,根据数据编码的不同,查询对应的码表,从而对字符进行高效操作的流对象。
字节流和字符流的区别:
- 读写单位不同:字节流以字节(8bit)为单位;字符流以字符为单位,根据码表映射字符,一次可能读取多个字节。
- 处理对象不同:字节流能处理所有类型的数据(如图片、视频等);字符流只能处理字符类型的数据。
- 字节流在操作的时候本身是不会用到缓冲区的,是对文件本身的直接操作;字符流在操作的时候是会用到缓冲区的,是通过缓冲区来操作文件。
结论:优先选用字节流。因为硬盘上所有的文件都是以字节的形式进行传输或保存的,但是字符只会在内存中形成,所以在开发中,字节流使用广泛。
- 输入流和输出流
读入写出,即对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。- 节点流和处理流
节点流是连接两个节点的最基本的流,处理流是处理节点流、增强其性能和可操作性的流。
IO流体系图
image.png
IO流对象
- FileInputStream和FileOutputStream
- BufferedInputStream和BufferedOutputStream
- DataInputStream和DataOutputStream
- ObjectInputStream和ObjectOutputStream
package leif; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Test { public static void main(String[] args) { FileInputStream fileInputStream = null; BufferedInputStream bufferedInputStream = null; ObjectInputStream objectInputStream = null; FileOutputStream fileOutputStream = null; BufferedOutputStream bufferedOutputStream = null; ObjectOutputStream objectOutputStream = null; try { File file = new File("C:\\Users\\Administrator\\Desktop\\test.txt"); fileInputStream = new FileInputStream(file); bufferedInputStream = new BufferedInputStream(fileInputStream); try { objectInputStream = new ObjectInputStream(bufferedInputStream); } catch (EOFException e) { // EOFException只是用作提示已经读到文档或对象流的结尾,只需捕获即可,无需做特殊处理 } fileOutputStream = new FileOutputStream(file); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); objectOutputStream = new ObjectOutputStream(bufferedOutputStream); Set<Student> studentSet = null; // 如果一开始文档中没有存储对象,那么objectInputStream将为空 if (objectInputStream == null) { // 如果objectInputStream为空,则说明此时文档中没有存储对象,那么新建一个对象即可 studentSet = new HashSet<Student>(); } else { // 如果objectInputStream不为空,则说明此时文档中已经存储了对象,那么获取该对象即可 studentSet = (Set<Student>) objectInputStream.readObject(); } System.out.println(studentSet); studentSet.add(new Student(1, "小明")); objectOutputStream.writeObject(studentSet); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectOutputStream != null) { try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedOutputStream != null) { try { bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (objectInputStream != null) { try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } class Student implements Serializable { private static final long serialVersionUID = 685129468499562521L; private int id; private String name; public Student(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Student other = (Student) obj; if (id != other.id) { return false; } if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } return true; } }
- ByteArrayInputStream和ByteArrayOutputStream
- PipedInputStream和PipedOutputStream
- FileReader和FileWriter
- BufferedReader和BufferedWriter
- ByteArrayReader和ByteArrayWriter
- PipedReader和PipedWriter