(i) on serialization(1) What is serialization
Turns a Java object into a byte stream or reverts from a byte stream to a Java object. For example, to store a Java object on a hard disk or to transfer it to another computer on the network, we can write our own code to convert a Java object into a byte stream of a format, but the JRE itself provides this support. We can call OutputStream's WriteObject method to do, if we want to let Java help us do, the object to be transferred must implement the serializable or Externalizable interface, so Javac compile will be special processing, The compiled class can be manipulated by the WriteObject method, which is called serialization.
(2) The role of serialization
When passing, and saving objects (object), it guarantees the integrity and transitivity of the object. For example, when transmitting over a network, or storing an object as a file, the serialization interface is implemented. For example
① in web development, if the object is stored in the session, Tomcat will need to implement the serializable or externalizable interface to serialize the session object to the hard disk when it restarts.
② if an object is to be transmitted over a distributed system or remotely via an RMI, it is necessary to transfer objects on the network, and the transmitted objects must implement the serializable or externalizable interfaces.
(3) Use of serialization
Classes that need to be serialized can implement one of two interfaces: the ① implements the serializable interface, and ② implements the Externalizable interface.
(ii) method one: Implementation of the Serializable interface(1) Non-custom
① defines a person class: you do not need to initialize the object through the constructor
Public classPersonImplementsSerializable {Private Static Final LongSerialversionuid = 1L; PrivateString name; Private intAge ; PublicPerson (String name,intAge ) { This. Name =name; This. Age =Age ; }//Omit Get, Set ... ..@Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + "]"; }}io method of ②java
/*** Serialization of Java Objects*/@Test Public voidwriteobjecttest () {Try{objectoutputstream Oos=NewObjectOutputStream (NewFileOutputStream ("D:/running_copy.txt")); Oos.writeobject (NewPerson ("Winson", 26)); Oos.writeobject (NewPerson ("Tom", 35)); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } /*** Deserialize read Java object*/@Test Public voidreadobjecttest () {ObjectInputStream ois=NewObjectInputStream (NewFileInputStream ("D:/running_copy.txt")); //Read sequentiallySystem.out.println (person) ois.readobject ()); SYSTEM.OUT.PRINTLN (person) ois.readobject ()); }
(2) Custom serialization person provides ReadObject and WriteObject methods to save the person object in a method, and the method of serialization and deserialization is consistent
Private void WriteObject (Java.io.ObjectOutputStream out) { out.writeobject (new StringBuffer (name). reverse ()); Out.writeint (age); } privatevoid readobject (Java.io.ObjectInputStream in) { this. Name=(((StringBuffer) In.readobject ()). reverse ()). ToString (); this. age=in.readint (); }
(iii) method Two: Implement the Externalizable interface, rewrite two interfaces (similar to the above custom serialization), must provide a parameterless constructor
Public classStudentImplementsexternalizable {Private Static Final LongSerialversionuid = 1L; PrivateString name; Private intAge ; //The parameterless constructor must be supplied PublicStudent () {} PublicStudent (String name,intAge ) { This. Name =name; This. Age =Age ; }//Omit Get, set method@Override Public voidReadexternal (ObjectInput in)throwsIOException, ClassNotFoundException { This. Name =((( stringbuffer) In.readobject ()). reverse ()). ToString (); This. Age =in.readint ();} @Override Public voidWriteexternal (ObjectOutput out)throwsIOException {Out.writeobject (NewStringBuffer (name). reverse ()); Out.writeint (age);} @Override PublicString toString () {return"Student [name=" + name + ", age=" + Age + "]"; }}
(iv) Summary (1) serizlizable
① no need to provide a constructor
② enables custom serialization, providing WriteObject, readobject methods
(2) Externalizable
① must provide an argument-free constructor
② must rewrite writeexternal, readexternal
(3) Outputsteam and Inputsteam
① Creating a Stream
New ObjectOutputStream (new FileOutputStream ("D:/running_copy.txt")); Input
New ObjectInputStream (new FileInputStream ("D:/running_copy.txt")); Output
② input Serializable object, WriteObject ()
Oos.writeobject (new person ("Winson"),oos.writeobject ( "Tom", "+"); o Os.writeobject(new Student ("Hello", +)); Oos.writeobject (new Student ("Mary") , 80));
③ output Serializable objects : ReadObject () must be read in write order
SYSTEM.OUT.PRINTLN (person) ois.readobject ()); SYSTEM.OUT.PRINTLN (person) ois.readobject ()); System.out.println ((Student) ois.readobject ()); System.out.println ((Student) ois.readobject ());
Object serialization for Java