First, what is serialization and deserialization:
Serialization: Refers to the process of converting objects in the Java heap memory into bytes (binary streams). That is, the Java object is stored in disk memory in some way, a process called serialization
Deserialization: The process of reverting an object on disk to a Java object.
Ii. advantages of serialization and deserialization:
1, in the distributed system, the object needs to be transferred in the network, the object needs to be converted into binary stream form, and the JavaBean object needing to be shared needs to be serialized.
2, Server passivation, the server found some objects for a long time no activity, the server will store some objects in local memory, need to go to the memory to find, and then deserialized into Java objects, save server memory.
Summary: ①, the implementation of data persistence, by serialization saved on the local hard disk. ②, the use of serialization to achieve the remote communication.
Third, the necessary conditions for serialization:
1, need to implement Java.lang.Serializable interface, Java Most of the classes are implemented class interface. Example: String Integer
2, the fault will judge, the current object is Serializable instance, only allow to do serialization, Java object instanceof Serializable to judge
3. API in JDK:
The WriteObject () method in ObjectOutputStream to serialize operations
The ReadObject () method in ObjectInputStream to deserialize the operation
JavaBean If the Java.io.Serializable interface is not implemented
Demo
Package Com.liuxn.serialization.javabean;import java.io.serializable;/** * Created by Administrator on 2018/6/13. * Create a JavaBean implementation Serializable interface */public class User implements Serializable {//The field that needs to be serialized private String name; private int age; Transient private string classname;//does not need to serialize the field public string GetName () {return name; } public void SetName (String name) {this.name = name; } public int Getage () {return age; } public String GetClassName () {return className; } public void Setclassname (String className) {this.classname = ClassName; public void Setage (int.) {this.age = age; }//Public User (String name, int.) {//this.name = name;//this.age = age;//}//////Public user () {//} @Override Public String toString () {return "user{" + "Name= '" + name + ' \ ' + ", age=" + Age + ", classname= '" + classnAme + ' \ ' + '} '; }}
Package Com.liuxn.serialization.test;import com.liuxn.serialization.javabean.user;import java.io.*;/** * Created by Administrator on 2018/6/13. * * For testing serialization and deserialization test class */public class Testserialization {public static void main (string[] args) {/** * makes Serialization with ObjectOutputStream */user user = new user (); User.setage (10); User.setname ("Xiao Ming"); SYSTEM.OUT.PRINTLN (user); ObjectOutputStream oos = null; try {outputstream op = new FileOutputStream ("F:\\360wifi" + file.separator+ "a.txt"); Oos = new ObjectOutputStream (OP); Oos.writeobject (user); Oos.close (); } catch (IOException e) {e.printstacktrace (); }/** * ObjectInputStream implements deserialization */try {inputstream in = new FileInputStream (" F:\\360wifi "+ file.separator+" a.txt "); ObjectInputStream OS = new ObjectInputStream (in); byte[] buffer = new BYTE[10]; int len =-1; User U = (user) os.readobject (); System.out.println ("deserialization:"); SYSTEM.OUT.PRINTLN (U); Os.close (); }catch (Exception e) {e.printstacktrace (); } }}
Output: User{name= ' Xiaoming ', age=10} deserialization: User{name= ' xiaoming ', age=10}
At serialization time, if JavaBean is finished serializing, modifying JavaBean, the following issues occur in deserialization:
Solve:
Add a serialversionuid fixed version to the JavaBean bar.
Private static final long serialversionuid = 1L;
JavaBean
Java Basic Order--serialization and deserialization