1. Concept
The "object serialization" of Java converts an object that implements the serializable interface into a set of byte, so that it can be recovered from the byte data and rebuilt on that object later.
Object serialization enables "lightweight persistence (lightweight persistence)". The so-called persistence is that the life cycle of an object is not determined by whether the program is running, and the object is still alive between the two invocations of the program. The persistence effect can be achieved by writing the serialized object to disk and then reading it again when the program is running again.
The reason to include object serialization in a language is to use it to implement two important functions:
A 1.Java remote method call can invoke objects on other machines as if it were an object on its own machine.
2. For JavaBean, the state information of the bean is usually configured at design time, and the state information must be saved for use by the program when it is started, and the object serialization is responsible for this work.
Object serialization not only saves a copy of the object, but also preserves the objects it references by following the reference in the object, and then continues to track the reference of those objects.
2. Implementing serialization
(1) Serialize an object just let it implement the Serializable interface (this is a "tag interface", tagging interface, there is no method).
(2) To serialize an object, you must first create a outputstream and then insert it into the objectoutputstream. This allows the object to be written to OutputStream using the WriteObject () method.
(3) When reading, you need to embed InputStream into the ObjectInputStream, and then call the ReadObject () method.
3. Examples
1 PackageEntity;2 3 Importjava.io.Serializable;4 5 Public classEmployeeImplementsserializable{6 7 /*8 * Serialversionuid Effect:9 * In order to maintain version compatibility when serializing, that is, deserialization still preserves the uniqueness of the object during version upgrade. Ten * If your class serialized is stored on the hard drive, but later you change the category field (increase or decrease or rename), One * When you deserialize, there will be exception, which will cause the problem of incompatibility. A * But when the serialversionuid is the same, it will deserialize the different field with the default value of type, which avoids the incompatibility problem. - */ - Private Static Final LongSerialversionuid = -5167690587890199758l; the - PublicString name; - PublicString address; - Public transient intSSN; + Public intNumber ; - PublicDepartment Department; + A PublicString toString () { at return"Employee [name=" + name + ", address=" + address + ", number=" + number + "]"; - } - -}Employee.java
1 PackageEntity;2 3 Importjava.io.Serializable;4 5 Public classDepartmentImplementsserializable{6 Private Static Final LongSerialversionuid = -6913084898834515327l;7 8 Public intID;9 PublicString name;Ten One PublicString toString () { A return"Department [id=" + ID + ", name=" + name + "]"; - } - the}Department.java
1 PackageTest;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 ImportJava.io.ObjectInputStream;8 ImportJava.io.ObjectOutputStream;9 Ten Importentity.department; One ImportEntity.employee; A - Public classTest1 { - the /** - * Serialized Object - */ - @org. Junit.test + Public voidSerializedemo () { -Employee e =NewEmployee (); +E.name = "Reyan Ali"; Ae.address = "Phokka Kuan, Ambehta Peer"; atE.SSN = 11122333; -E.number = 101; -Department d =NewDepartment (); -D.id = 1; -D.name = "Java development"; -E.department =D; in Try { - System.out.println (e); toFileOutputStream fileout =NewFileOutputStream (NewFile ("/users/zhengbinmac/employee.txt")); +ObjectOutputStream out =NewObjectOutputStream (fileout); - System.out.println (e); the Out.writeobject (e); * out.close (); $ fileout.close ();Panax NotoginsengSystem.out.printf ("Serialized data is saved In/users/zhengbinmac/employee.txt"); -}Catch(IOException i) { the i.printstacktrace (); + } A } the + /** - * Deserializing Objects $ */ $ @org. Junit.test - Public voidDeserializedemo () { -Employee e =NULL; the Try { -FileInputStream Filein =NewFileInputStream ("/users/zhengbinmac/employee.txt");WuyiObjectInputStream in =NewObjectInputStream (Filein); theE =(Employee) in.readobject (); - in.close (); Wu filein.close (); -}Catch(IOException i) { About i.printstacktrace (); $ return; -}Catch(classnotfoundexception c) { -System.out.println ("Employee Class not Found"); - c.printstacktrace (); A return; + } theSystem.out.println ("Deserialized Employee ..."); -System.out.println ("Name:" +e.name); $System.out.println ("Address:" +e.address); theSystem.out.println ("SSN:" +e.ssn); theSystem.out.println ("Number:" +e.number); theSystem.out.println ("Department:" +e.department); the } -}
One day a Java Foundation-serialization