Java serialization
Java provides a mechanism for object serialization, in which an object can be represented as a sequence of bytes that includes the object's data, information about the type of the object, and the type of data stored in the object.
After a serialized object is written to a file, it can be read from the file and deserialized, that is, the object's type information, the object's data, and the data type in the object can be used to create a new object in memory.
Classes ObjectInputStream and ObjectOutputStream are high-level data streams that contain methods for serializing and deserializing objects.
What happens when serialization is required
A) When you want to save the object in memory in a file or in a database ;
b) When you want to use sockets to transfer objects on the network;
c) When you want to transfer objects through RMI;
Serialization ID
The serialization ID provides two build strategies under Eclipse, one fixed 1L, one randomly generating a non-repeating long type of data (actually generated using the JDK tool), where there is a recommendation that, if there is no special requirement, it is available with the default 1L, which ensures that the code is consistent When deserialization succeeds. This can also be the cause of serialization and deserialization failures because different serialization IDs cannot be serialized and deserialized.
Attention:
The class must implement the Java.io.Serializable object.
All properties of the class must be serializable. If there is a property that is not serializable, the attribute must be noted to be short-lived.
Summarize:
A) When a parent class is serialized and the subclass is automatically serialized, no explicit implementation of the serializable interface is required;
b) When an instance variable of an object refers to another object, the object is serialized by serializing it;
c) The variable after static,transient cannot be serialized;
Examples show:
Public class Serializationtest {
public static void Main (string[] args) {
Employee employee = New Employee ("Brook", "Shenyang", 100506);
final String filePath = "/tmp/employee.ser";
//Serialization
try {
fileoutputstream outputstream = new FileOutputStream (filePath);
objectoutputstream objstream = new ObjectOutputStream (outputstream);
objstream.writeobject (employee);
objstream.close ();
outputstream.close ();
} catch (IOException e) {
e.printstacktrace ();
}
//Deserialization
Employee reademp = null;
try {
fileinputstream input = new FileInputStream (filePath);
objectinputstream objin = new ObjectInputStream (input);
reademp = (Employee)objin.readobject ();
input.close ();
objin.close ();
} catch (IOException e) {
e.printstacktrace ();
return;
} catch (ClassNotFoundException e) {
e.printstacktrace ();
return;
}
System. out.println ("Name:" + reademp.getname ());
System. out.println ("number:" + Reademp.getnumber ());
System. out.println ("ZIP code:" + Reademp.postcode ());
}
}
Output Result:
Name: Brook
Item No: 99
P.C.: 0
Java Serialization notes