序列化流
-
只有序列化字节流,参数也只能是字节流
-
对象的序列化:将对象转为字节码表示。
-
对象的反序列化:将字节码转为对象。
-
序列化流 :ObjectOutputStream
void writeObject(Object o); 将指定对象写入文件
//类要实现序列化,反序列化必须实现Serializable接口,否则会出现异常
Student s1=new Student("王",18);
//序列化
FileOutputStream f2=new FileOutputStream("dui.txt");
ObjectOutputStream f4=new ObjectOutputStream(f2);
f4.writeObject(s1);
反序列化流
- 反序列化流:ObjectInputStream
Object readObject(); 从文件中读出对象;
FileInputStream f1=new FileInputStream("dui.txt");
ObjectInputStream f3=new ObjectInputStream(f1);
Student ss=(Student)f3.readObject();
- 序列化对象的类要实现接口:Serializable
序列化时可能会发生异常:InvalidClassException
可以手动给类添加一个序列号格式 private static final long serialVersionUID = 111; 解决异常