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.
The entire process is independent of the Java Virtual Machine (JVM), which means that objects serialized on one platform can deserialize the object on another completely different platform.
Classes ObjectInputStream and ObjectOutputStream are high-level data streams that contain methods for serializing and deserializing objects.
Example:
The entity class that realizes the serialized interface serializable
PackageCom.serialize;Importjava.io.Serializable;/** * Employee Employee class, and implements the Serializable serialization interface * Note that the object of a class must satisfy two conditions to serialize successfully: * 1, the class must implement a Java.io.Serializable object. * 2, 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. * @author Feizi * @time 2015-1-19 pm 6:01:11 * * Public class Employee implements Serializable { Private Static Final LongSerialversionuid =-6290787164737383314L PublicString name; PublicString address; Public transient intSSN; Public intNumber Public void MailCheck() {System.out.println ("Mailing a check to"+name+" "+address); }}
Second, serialization
PackageCom.serialize;ImportJava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.IOException;ImportJava.io.ObjectOutputStream;/** * Java serialized object * @author Ruanpeng * @time 2015-1-19 pm 6:00:29 * * Public class serializedemo { Public Static void Main(string[] args) {Employee E =NewEmployee (); E.name ="Reyan Ali"; E.address ="Phokka Kuan, Ambehta Peer"; E.SSN =111222333; E.number =101;The //objectoutputstream class is used to serialize an object Try{//When serializing an object to a file, the standard convention in Java is to give the file a. ser extension. //1, directly written in the following way, but the premise that the TMP folder to be created well in advance (already exist), or run will be reported to find the path errorFileOutputStream fileout =NewFileOutputStream ("Tmp/employee.ser");//2, or do not have the previous TMP folder, directly write the file name, you can//FileOutputStream fileout = new FileOutputStream ("Employee.ser"); ///3, or use the file class provided by the Java API itself to create a folder path, then the above code can be replaced by the following /*file tmpfolder = new File ("/tmp"); if (!tmpfolder.exists ()) {Tmpfolder.mkdir (); } fileoutputstream fileout = new FileOutputStream (tmpfolder+ "/employee.ser"); */ObjectOutputStream out =NewObjectOutputStream (fileout); Out.writeobject (e); Out.close (); Fileout.close (); System.out.printf ("serialized data is saved in Tmp/employee.ser"); }Catch(FileNotFoundException E1) {E1.printstacktrace (); }Catch(IOException E1) {E1.printstacktrace (); } }}
Third, deserialization
PackageCom.serialize;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.IOException;ImportJava.io.ObjectInputStream;/** * Deserialization Object * @author Ruanpeng * @time 2015-1-19 pm 6:13:56 * * Public class deserializedemo { Public Static void Main(string[] args) {Employee E =NULL;Try{//deserialization, the path is the same as when serializing the path. FileInputStream Filein =NewFileInputStream ("Tmp/employee.ser"); ObjectInputStream in =NewObjectInputStream (Filein); E = (Employee) in.readobject (); In.close (); Filein.close (); }Catch(FileNotFoundException E1) {E1.printstacktrace (); }Catch(IOException E1) {E1.printstacktrace ();return; }Catch(ClassNotFoundException E1) {System.out.println ("Employee class not found"); E1.printstacktrace ();return; }//When the object is serialized, the value of the property ssn is 111222333, but because the property is ephemeral, the value is not sent to the output stream. So the SSN property of the Employee object after deserialization is 0. //The above explanation is in the information, it is not particularly understood. System.out.println ("Deserialize Employee ..."); System.out.println ("Name:"+e.name); System.out.println ("Address:"+e.address); System.out.println ("SSN:"+E.SSN); System.out.println ("Number:"+e.number); }}
Iv. operation Effect of the program
OK, end ...
The serialization of Java Learning