显示声明serialVersionUID可以避免对象不一致
实例1:有serialVersionUID
序列化与反序列实体类
public class Person2 implements Serializable{ /** * */ private static final long serialVersionUID = 1L; /** * */ public Long id; public String name; public final String userName; public String sex; public int age; public Person2(Long id, String name,String sex,int age){ this.id = id; this.name = name; userName = "dddbbb"; this.sex=sex; this.age=age; } public String toString() { return id.toString() + "--" + name.toString(); } }
序列化类:
public class SerialTest { public static void main(String[] args) { Person2 p = new Person2(1L, "陈俊生","male",22); System.out.println("person Seria:" + p); try { FileOutputStream fos = new FileOutputStream("Persion.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(p); oos.flush(); oos.close(); } catch (IOException e) { e.printStackTrace(); } } }
序列化执行完成之后,反序列化执行之前,如果更改了实体类Person后,在执行反序列化代码
public class DeserialTest { public static void main(String[] args) { Person2 p; try { FileInputStream fis = new FileInputStream("Persion.txt"); ObjectInputStream ois = new ObjectInputStream(fis); p = (Person2) ois.readObject(); ois.close(); System.out.println(p.toString()); System.out.println(p.userName); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }
结果没有异常
实例化2:无serialVersionUID
public class Person2 implements Serializable{
/**
*
*
/**
*
*/
public Long id;
public String name;
public final String userName;
public String sex;
public int age;
pulic String school;/////序列化之后,反序列化之前新增字段
public Person2(Long id, String name,String sex,int age){
this.id = id;
this.name = name;
userName = "dddbbb";
this.sex=sex;
this.age=age;
}
public String toString() {
return id.toString() + "--" + name.toString();
}
}
序列化类:
public class SerialTest {
public static void main(String[] args) {
Person2 p = new Person2(1L, "陈俊生","male",22);
System.out.println("person Seria:" + p);
try {
FileOutputStream fos = new FileOutputStream("Persion.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(p);
oos.flush();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
序列化执行完成之后,反序列化执行之前,如果更改了实体类Person后(本例中新增字段String school),在执行反序列化代码
public class DeserialTest {
public static void main(String[] args) {
Person2 p;
try {
FileInputStream fis = new FileInputStream("Persion.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
p = (Person2) ois.readObject();
ois.close();
System.out.println(p.toString());
System.out.println(p.userName);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
此时反序列化代码抛出异常:
java.io.InvalidClassException: serial.Person2; local class incompatible: stream classdesc serialVersionUID = 5850213951562638361, local class serialVersionUID = -1986140618597495916
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:699)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1885)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
at serial.DeserialTest.main(DeserialTest.java:13)