1. Scene description: recently I was watching serialization and encountered some doubts
//
public class parent implements Serializable{
private static final long serialVersionUID = 7604030015683112269L; //UID
private String name;
//setter/getter...
}
//
public class Child extends parent {
//private String attr1;//
}
//
public class Test{
public static void main(String[] args) throws Exception{
//serializeTest(); //
Child c = deserializeTest();
System.out.println(c.toString());
}
/**/
public static void serializeTest() throws IOException {
Child c= new Child();
c.setName("wyy");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("d:/test.txt")));
oos.writeObject(c);
System.out.println(c.toString());
System.out.println("success");
} catch (IOException e) {
e.printStackTrace();
}finally {
oos.close();
}
}
/**/
public static Rich deserializeTest() throws IOException {
Child c = null;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(new File("d:/test.txt")));
c = (Child) ois.readObject();
System.out.println("");
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
ois.close();
}
return c;
}
}
2. Problem description:
a.
b.
c. InvalidClassExceptionUID..
d.ID
("" "" ""
)
3. Doubt:
ID
@TransientInvalidClassException...
Why is there an error if no serialVersionUID, subclass adds new attributes and then deserializes in the test case, while the attributes of the class are often added in the development, and there is no serialVersionUID, in the class but no error is reported? Is it because you connected to the database?
=
I hope the boss who knows about this will give us some advice. Thank you