1.序列化流/对象操作输出流:可以把Java中的对象写到本地文件中
序列化流细节:使用对象输出流将对象保存到文件时会出现NotSerializableException
解决方案:需要让JavaBean类实现Serializable接口
Serializable接口里面没有抽象方法--->标记型接口
一旦实现了这个接口,那么就表示当前的Student类可以被序列化
序列化流写出一个对象代码演示:
Student类:
package a05objectstreamdemo;
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
}
测试类:
package a05objectstreamdemo;
import java.io.*;
public class ObjectStreamDemo1 {
public static void main(String[] args) throws IOException {
//利用序列化流把一个对象写到本地文件中
//1.创建对象
Student stu=new Student("zhangsan",23);
//2.创建序列化流的对象/对象操作输出流
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("Test\\a.txt"));
//3.写出数据
oos.writeObject(stu);
oos.close();
}
}
2.反序列化流/对象操作输入流:可以把序列化到本地文件中的对象,读取到程序中来
代码演示:
package a05objectstreamdemo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ObjectStreamDemo2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1.创建反序列化流的对象
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("Test\\a.txt"));
Object o = (Student)ois.readObject();
System.out.println(o);
ois.close();
}
}
运行结果:
3.细节:
①序列化流写到文件中的数据是不能修改的,一旦修改就无法再次读回来了
②序列化对象后,修改了JAvaBean类,再次反序列化会出问题,会抛出InvalidClassException异常
解决方案:给JAvaBean类添加serialVersionUID(序列号、版本号)
③如果一个对象中的某个成员变量的值不想被序列化,
解决方案:给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程
读取时该变量默认初始化值
4.练习:用对象流读写多个对象
需求:将多个自定义对象序列化到文件中,但是由于对象的个数不确定,反序列化流该如何读取呢
Student类:
package a05objectstreamdemo;
import java.io.Serial;
import java.io.Serializable;
public class Student1 implements Serializable {
@Serial
private static final long serialVersionUID = 3056031205966689519L;
private String name;
private int age;
private String adress;
public Student1() {
}
public Student1(String name, int age, String adress) {
this.name = name;
this.age = age;
this.adress = adress;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String toString() {
return "Student1{name = " + name + ", age = " + age + ", adress = " + adress + "}";
}
}
序列化对象:
package a05objectstreamdemo;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class ObjectStreamDemo3 {
public static void main(String[] args) throws IOException {
//1.序列化多个对象
Student1 s1=new Student1("zhangsan",23,"南京");
Student1 s2=new Student1("lisi",24,"重庆");
Student1 s3=new Student1("wangwu",25,"北京");
ArrayList<Student1> list=new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("Test\\a.txt"));
oos.writeObject(list);
oos.close();
}
}
反序列化对象:
package a05objectstreamdemo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class ObjectStreamDemo4 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1.创建反序列化流的对象
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("Test\\a.txt"));
//2.读取数据
ArrayList<Student1> list = (ArrayList<Student1>) ois.readObject();
for (Student1 student1 : list) {
System.out.println(student1);
}
ois.close();
}
}
运行结果: