Loading old versions of an object from a file

What happens if I try to load an older version of an object from a file?

For example, if I created an object, I saved this object in a file using FileOutputStream. Then, if I added a field and tried to reload this object, Java would not recognize the object at all or set the fields in the default field or ...?

Thanks:)

+4
source share
2 answers

When trying to deserialize a class with a different version of the class, you should get an InvalidClassException . From javadocs :

Serialization of serialization is associated with each serializable class, a version number called serialVersionUID, which is used during deserialization to verify that the sender and receiver of the serialized object have loaded classes for this object that are compatible with regards to serialization. If the receiver has loaded the class for an object with a different serialVersionUID than the corresponding sender class, then deserialization will result in an InvalidClassException. A serializable class can declare itself serialVersionUID explicitly by declaring a field named "serialVersionUID", which must be static, final, and long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L; If the serializable class does not explicitly declare serialVersionUID, then the serialization execution time will be calculated by default serialVersionUID for this class based on various aspects of the class, as described in the serialization of the Java (TM) specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default calculation of serialVersionUID is very sensitive to the class details, which may vary depending on compiler implementations and may thus lead to unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value in different java compiler implementations, the serializable class must declare an explicit serialVersionUID value. It is also strongly recommended that explicit declarations of serialVersionUID be used whenever possible, since such declarations apply only to immediately declaring classes - serialVersionUID fields are not useful as inherited.

+1
source

Most likely you will get an exception because the serialVersionUID stored in the file does not match the instance in the current version of the class.

By default, Java just throws errors.

If you want to provide a way to read "old versions", you must implement the readObject method .

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

 private void writeObject(java.io.ObjectOutputStream out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; private void readObjectNoData() throws ObjectStreamException; 
+1
source

Source: https://habr.com/ru/post/1492000/


All Articles