正常不设置serialVersionUID 的序列化和反序列化
先定义一个实体Student.class
,需要实现Serializable
接口,但是不需要实现get(),set()方法
import java.io.Serializable;
public class Student implements Serializable {
private int age;
private String name;
public Student(int age, String name) {
this.age = age;
= name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
测试类,思路是先把Student对象序列化到Student.txt
文件,然后再讲Student.txt
文件反序列化成对象,输出。
public class SerialTest {
public static void main(String[] args) {
serial();
deserial();
}
// 序列化
private static void serial(){
Student student = new Student(9, "Mike");
try {
FileOutputStream fileOutputStream = new FileOutputStream("Student.txt");
ObjectOutputStream objectOutputStream= new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(student);
objectOutputStream.flush();
} catch (Exception exception) {
exception.printStackTrace();
}
}
// 反序列化
private static void deserial() {
try {
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Student student = (Student) ois.readObject();
ois.close();
System.out.println(student.toString());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
输出结果,序列化文件我们可以看到Student.txt
,反序列化出来,里面的字段都是不变的,说明反序列化成功了。
序列化之后,类文件增加了字段,反序列化会怎么样?
先说结果,会失败!!!
我们在Student.java
中增加了一个属性score
,重新生成了toString()
方法。
package com.aphysia.normal;
import java.io.Serializable;
public class Student implements Serializable {
private int age;
private String name;
private int score;
public Student(int age, String name) {
this.age = age;
= name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", score=" + score +
'}';
}
}
然后重新调用deserial()
方法,会报错:
java.io.InvalidClassException: com.aphysia.normal.Student; local class incompatible: stream classdesc serialVersionUID = 7488921480006384819, local class serialVersionUID = 6126416635811747983
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:699)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1963)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1829)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2120)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1646)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:482)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:440)
at com.aphysia.normal.SerialTest.deserial(SerialTest.java:26)
at com.aphysia.normal.SerialTest.main(SerialTest.java:7)
从上面的报错信息中,我们可以看到,类型不匹配,主要是因为serialVersionUID
变化了!!!