The serialization mechanism allows you to convert a serialized Java object into a sequence of bytes that can be saved on disk or transmitted over a network for later restoration to the original object. The serialization mechanism allows an object to exist independently from the running of the program.
The serialization of Objects (Serialize) refers to the writing of a Java object to the IO stream, which corresponds to the object's deserialization (deserialize), which is the restoration of the Java object from the IO stream. If you need to have an object that can support the serialization mechanism, you must make its class serializable (serializable), in order for a class to be serializable, the class must implement one of the following two interfaces:
(1) Serializable
(2) Externalizable
Once a class implements the Serializable interface, the object of the class is serializable, and the program can serialize the object in the following two steps.
Steps to serialize:
(1) Create a objectoutputstream, this output stream is a processing stream, so it must be built on the basis of other node flows.
(2) Call the WriteObject method of the ObjectOutputStream object to output the serializable object.
Steps to Deserialize:
(1) Create a objectinputstream, this input stream is a processing stream, so it must be built on the basis of other node flows.
(2) The ReadObject object of the ObjectInputStream object is called to read the object in the stream, which returns a Java object of the type object and, if the program knows the type of the Java object, it can be cast to its true type.
The following is just an example of implementing the Serializable interface to demonstrate the process of serialization and deserialization in a complete instance:
1. Person.java
Import Java.io.serializable;public class Person implements serializable{private String Name;private int age;public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public person (String name, int.) {super (); this.name = Name;this.age = Age;}}
2.writeperson.java
Import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.objectoutputstream;public class Writeperson {public static void main (string[] args) {// Create ObjectOutputStream (process flow, must be built on top of other node streams) object try {objectoutputstream oos=new objectoutputstream (New FileOutputStream (" Per.txt ")); Person Per=new person ("Zhang San"); Oos.writeobject (per); Oos.close ();} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();}}}
3.Readperson. Java
public class Readperson {public static void main (string[] args) {try (ObjectInputStream ois=new objectinputstream (New File InputStream ("Per.txt")) {person per= (person) ois.readobject (); System.out.println (Per.getname () + "-----------" +per.getage ());} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
In Java EE, it is generally required that each JavaBean be serializable. The serialization mechanism is very important in the development of Java EE. Hope can arouse readers ' attention.
Serialization and deserialization of Java Objects (1)