Related definitions and uses
The Java serialization (also called serialization) technique allows you to write the state of an object into a byte stream, and you can read the data in that byte stream from somewhere else and reconstruct the same object. This mechanism allows you to propagate objects through the network, and can persist objects to databases, files and other systems at any time. The serialization mechanism of Java is the Technical foundation of RMI (remote method call), EFB and so on.
Purpose: Use the serialization of the object to save the current working state of the application, and the next time it is started, it will automatically revert to the last executed state.
Serialization is a mechanism for dealing with the flow of objects, so-called object flow is the flow of the object's contents. It is possible to read and write to a Fluidized object, or to transfer the streamed object between the networks. Serialization is a problem that is raised when reading and writing to an object stream.
Realization method and its characteristics
Implementation method: implement the Serializable interface for the class that needs to be serialized, and then use an output stream (for example: FileOutputStream) to construct an Objectoutpotstream (object flow) object, and then Using the WriteObject (Object obj) method of the Objectoutputtream object, you can write out (that is, save its state) the object with the parameter obj, and then use the input stream to recover.
Features of serialization:
- If a class can be serialized, its subclasses can also be serialized. If the class has a parent class, it is considered in two cases if the parent class has implemented a serializable interface. The corresponding fields and properties of the parent class are treated the same as the class, and if the parent class of the class does not implement a serializable interface, all field properties of the class's parent class will not be serialized.
- member data declared as static and transient types cannot be serialized. Because static represents the state of a class, transient represents the temporary data for the object.
- Related mines and interfaces: the serialized classes of design objects provided in the Java.io package have objectoutput interfaces with interfaces, ObjectOutputStream classes, objectinput interfaces, ObjectInputStream classes.
ObjectOutput interface: Inherits the DataOutput interface and supports the serialization of objects, in which the WriteObject () method implements the storage of an object.
ObjectInput interface: Inherits the Datainput interface and supports the serialization of the object, and the ReadObject () method in it implements reading an object.
ObjectOutputStream class: Inherits the Outputstram class and implements the ObjectOutput interface. This class is used to implement object storage.
ObjectInputStream class: Inherits the Inputstram class and implements the ObjectInput interface. Use this class to implement reading an object.
For handling of parent classes:
If the parent class does not implement a serialized interface, it must have a default constructor (that is, an argument-free constructor). Otherwise, the compilation will be error. Because the default paparazzi function is called when deserializing. When Java deserializes a serialized object, it takes the object data directly from the stream to generate an object instance instead of the constructor.
An example of a serialized object:
ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;Importjava.io.Serializable;@SuppressWarnings("Serial") Public class person implements Serializable{ PrivateString name;Private intAge PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } Public Static void Main(string[] args) {Person person =NewPerson (); Person.setname ("SjS"); Person.setage ( -);Try{FileOutputStream FOS =NewFileOutputStream ("Sjs.out"); ObjectOutputStream Oos =NewObjectOutputStream (FOS); Oos.writeobject (person); Oos.close (); }Catch(Exception e) {//TODO auto-generated catch blockE.printstacktrace (); }Try{FileInputStream FIS =NewFileInputStream ("Sjs.out"); ObjectInputStream Ois =NewObjectInputStream (FIS); Person P = (person) ois.readobject (); System.out.println (P.getname ()); System.out.println (P.getage ()); Ois.close (); }Catch(Exception e) {//TODO auto-generated catch blockE.printstacktrace (); } }}
Operation Result:
Serialization is very useful in Android development.
A serialized explanation of the Java basics