也叫过滤流类处理刘类
没有对应到任何具体的流设备,需要给它传递一个对应的具体流设备的输出/输入流对象
I/0内存缓冲
BufferedInputStream,BufferedOutputStream 缓冲区包装类 默认32个字节缓冲区的缓冲流
内存/磁盘扇区一次读写操作所能完成最大字节数的整数倍(4的整数倍)
BufferedReader的readLine一次读取一行文本。
BufferedWriter的newLine可向字符流中写入不同操作系统下的换行符
DEMO:
import java.io.*;
class DataStreamTest{
public static void main(String[] args) throws Exception{
FileOutputStream fos=new FileOutputStream("count.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);//包装fos
DataOutputStream dos= new DataOutputStream(bos);//包装bos
dos.writeUTF("china中国");
dos.writeBytes("china中国");
dos.writeChars("china中国");
dos.close();
//读取
FileInputStream fis=new FileInputStream("count.txt");
BufferedInputStream bis=new BufferedInputStream(fis);//包装fis
DataInputStream dis= new DataInputStream(bis);
System.out.println(dis.readUTF());
byte[] buf=new byte[1024];
int len=dis.read(buf);
System.out.println(new String(buf,0,len));
fis.close();
}
}
ObjectInputStream,ObjectOutputStream类
用于从底层输入流中读取对象类型的数据和将对象类型的数据写入到底层输入流
必须实现Serializable接口才能实现读写对象。对象中的transient和static的成员变量不会被读取和写入。
网络流使用。
public class MyClass inplements Serializable{//可以避免不同系统间的差异
public transient Thread t;
private String customerID;
private int total;
}
DEMO:
import java.io.*; class Serialization{ public static void main(String[] args) throws Exception{ Student stu1=new Student(19,"zs",20,"ruanjian"); Student stu2=new Student(20,"ls",22,"wuli"); FileOutputStream fos=new FileOutputStream("student.txt"); ObjectOutputStream os=new ObjectOutputStream(fos); os.writeObject(stu1); os.writeObject(stu2); os.close(); //读取 FileInputStream fis=new FileInputStream("student.txt"); ObjectInputStream ois=new ObjectInputStream(fis); stu1=(Student)ois.readObject(); stu2=(Student)ois.readObject(); ois.close(); System.out.println("Id:"+stu1.id); System.out.println("name:"+stu1.name); System.out.println("age:"+stu1.age); System.out.println("department:"+stu1.department); System.out.println("Id:"+stu2.id); System.out.println("name:"+stu2.name); System.out.println("age:"+stu2.age); System.out.println("department:"+stu2.department); } }