In this chapter, we study and discuss the serialization in depth. Learning content, including the role of serialization, use, usage, and in-depth study of 2 ways to achieve serialization serializable and externalizable.
1. Function and use of serialization
serialization, in order to save the state of the object, and the corresponding deserialization, you can read the state of the saved object. In short: serialization/deserialization is a mechanism that Java provides for the state of a Save/restore object.
Generally in the following situations, we might use serialization: A When you want to save the state of an object in memory to a file or database, b when you want to use sockets to transfer objects over the network; c when you want to transfer objects through RMI.
2. Demo Program 1
Next, let's look at the use of serialization with a simple example.
The source code is as follows (Serialtest1.java):
/** * Serialization Demo Test program * * @author Skywang/import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import java.io.Serializable;
public class SerialTest1 {private static final String Tmp_file = ". Serialtest1.txt";
public static void Main (string[] args) {//Save Testwrite () by serialization of "objects";
Read the serialized "Objects" Testread ();
/** * The Box object is serialized and saved to the file/private static void Testwrite () {try {
Gets the corresponding object output stream for the file tmp_file.
ObjectOutputStream, you can only write to basic data or to support serialized objects ObjectOutputStream out = new ObjectOutputStream (
New FileOutputStream (Tmp_file));
To create the Box object, box implements the Serializable serialization interface Box box = new box ("desk", 80, 48);
Writing the Box object to the object output stream out, which is equivalent to saving the object to the file Tmp_file out.writeobject (box); PrintBox Object "System.out.println (" testwrite Box: "+ box);
Out.close ();
catch (Exception ex) {ex.printstacktrace (); }/** * read out "serialized box object" from File/private static void Testread () {try {//
Gets the corresponding object input stream for the file tmp_file.
ObjectInputStream in = new ObjectInputStream (new FileInputStream (Tmp_file));
Reads the previously saved box object from the object input stream.
Box box = (box) in.readobject ();
print "Box object" System.out.println ("testread Box:" + box);
In.close ();
catch (Exception e) {e.printstacktrace (); }}/** * Box class "support serialization".
Because box implements the serializable interface.
* In fact, a class only needs to implement serializable to achieve serialization without having to implement any functions.
* * Class Box implements Serializable {private int width;
private int height;
private String name; Public Box (String name, int width, int height) {this.name = nAme
This.width = width;
This.height = height;
@Override public String toString () {return "[+name+": ("+width+", "+height+")] "; }
}
Run Result:
Testwrite box: [Desk: (+)] testread box: [Desk: (80, 48)]