Reprint please specify the original address: http://www.cnblogs.com/ygj0930/p/6574920.html
One: Serialization and deserialization
Serialization: The process of translating a Java object into a sequence of bytes;
deserialization: The process of refactoring a Java object from a sequence of bytes.
Two: The role of serialization
1) Object Persistence: We know that objects are created as the program runs and then recycled when unreachable, and the life cycle is ephemeral. But what if we want to preserve the object's content for a long time? Convert it to a byte sequence and save it on the storage medium. Then serialization is required.
2) Network Transmission object: We know that when communicating between two processes, the transmitted audio, video and other information is transferred in the form of a binary sequence. So, is it okay to object? It is possible to serialize the object on the host a process to a binary sequence by serialization, and the process transferred to Host B reconstructs the object from the sequence. This is widely used in RMI, and the result of RMI can be an object.
Three: How Java serializes objects
1:java Default API for serialization
The obj object of a parameter can be serialized by using the writeobject (object obj) method of the Java.io.ObjectOutputStream (objects output stream) . Writes the resulting byte sequence to one of the output streams.
Reads a sequence of bytes from the source input stream through the ReadObject () method of the Java.io.ObjectInputStream (object input stream) , then deserializes them into an object and returns them. When returned, assigns a value to a specific class object reference by forcing the type conversion. (This class of information is interpreted as a binary sequence to reconstruct the object).
2: Three ways to allow objects to be serialized
1) Default serialization: the implementation of the serializable interface when defining a class, the Serializable interface is an empty interface, there is no method to implement. The effect is that the object that marks the class can be serialized, enabling its serialization functionality. The object is serialized and deserialized by calling the ObjectOutputStream and ObjectInputStream methods.
2) Class custom serialization: When defining a class, implement the Serializable interface and define it in the class
private voidwriteobject(Java.io.ObjectOutputStream out) throws IOException private voidReadObject(Java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; These two methods, in the methodcustom content output through object input stream parameters, object output stream parameters. This allows you to serialize and deserialize objects by using the input and output methods of the object output stream and the object input streamis automatically called in the class defined in theThe WriteObject,
and ReadObject methods instead of the default serialization and deserialization methods.
3) Class custom serialization mode two: Implement the externalnalizable interface, and implement Readexternal (ObjectInput in) and Writeexternal (objectoutput Out) method to define the class object customization in the method
Serialization and deserialization operations. this will automatically invoke the object defined in the class when serializing and deserializing the object through the input and output methods of the output stream and the object input stream.Readexternal (ObjectInput in) and Writeexternal (ObjectOutput out) methods.
Four: Using steps for serialization and deserialization
1: Create the class, implement the Serializable interface or Externalizable interface, implement the corresponding serialization and deserialization method (also can take the default method);
2: After creating an object in program code, create an object output stream ObjectOutputStream object and specify the output target of the stream in the construction parameters (such as a file), through Objectoutputstream.writeobject (obj ) The object is serialized and output to the stream target;
3: At the point where the object needs to be extracted: Create an object input stream ObjectInputStream object and specify the source of the stream in the construction parameter , then get the object through the ReadObject () method and assign a value to the class object reference by forcing the type conversion
V: Special case for Java serialization
VI: Other serialization means
Serialization and deserialization of Java