Serialization, serialization, and deserialization
1. What is serialization?
Writing an object in the memory to the hard disk is serialized, which is no different from the general output, but the output data is an object, not a general text.
2. Functions of serialization
Because the storage of data in the memory is temporary, if you need to save the object for a long time, you must write the object to the hard disk to produce serialization.
3. serialization Conditions
The class to which an object belongs must implement the Serializable interface. This interface is a symbolic interface without any fields or abstract methods, when JVM encounters this interface, it will assign a serialization version number to the class.
4. An object is serialized. global variables including private variables in this object will be written to the hard disk.
5. fields that cannot be serialized: 6. serialization operation:
OutputStream ops=new FileOuptStream(path);ObjectOuptStream oos=new ObjectOutputStream(ops);Object obj=new Ojbect();oos.writeObject(obj);
Binary deserialization 1. What is deserialization?
Deserialization is to write data in the hard disk to the memory to obtain objects stored in files.
2. deserialization operation requirements
The deserialization sequence must be consistent with the serialization sequence.
3. serialization version number
(1) A long-type number is generated during serialization, which is called the serialization version number and is saved in the serialization file and the class file. During deserialization, two numbers are compared. If they are the same, the serialization is successful; otherwise, deserialization fails.
(2) the serialization version number is used to mark the version of the object class. Once the object class is modified, if the serialization version number is not explicitly specified, the system automatically generates a new version number, unable to serialize. The serialization version number is explicitly set in the Entity class to be serialized.
(3) A new serialization version number may not be generated unless the object class is changed.
(4) parsing process
During deserialization, we first compare whether the serialization version number in the serialized file and the class file is the same. if the version number is the same, it indicates that the serialization file is generated by the current class file and can be deserialized, indicates that the current class file is not generated, and the version is inconsistent and cannot be deserialized.
Reverse Sequence operation:
InputStream is=new FileInputStream(path);ObjectInputStream ois=new ObjectIputStream(is);Object obj=ois.readObject();