关于ByteArrayInputStream、ByteArrayOutputStream 和 ObjectInputStream、ObjectOutputStream

ByteArrayInputStream  read

 

ByteArrayOutputStream   write 

实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray() 和 toString() 获取数据。

 

ObjectOutputStream

package demo;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
 
public class Demo1_ObjectOutputStream {
 
    public static void main(String[] args) throws IOException{
        Student s1=new Student("张三",23);
        Student s2=new Student("李四",24);
        ObjectOutputStream os=new     
                ObjectOutputStream(new FileOutputStream("record.txt"));
        os.writeObject(s1);
        os.writeObject(s2);
        os.close();
 
    }
 

 

ObjectInputStream 

反序列化流,将之前使用 ObjectOutputStream 序列化的原始数据恢复为对象,以流的方式读取对象。 

 // 创建反序列化流
 FileInputStream fileIn = new FileInputStream("employee.txt");
 ObjectInputStream in = new ObjectInputStream(fileIn);
 // 读取一个对象
 e = (Employee) in.readObject();

 

posted @ 2020-03-27 22:02  xajh00789  阅读(736)  评论(0编辑  收藏  举报