Serialization and deserialization of java Analysis

Read article directory

First, what is the serialization and de-serialization
Second, serialization and de-serialization major role in
three prerequisites to achieve serialization and de-serialization
Fourth, how to implement serialization and de-serialization
V. code implementation
Precautions

First, what is the serialization and de-serialization

Java serialization (Serialization): to convert a java object sequence of bytes

java deserialization (Anti-Serialization): new java object by the sequence of bytes in memory process.

This byte sequence contains: type information of an object, the object data and object type data.

Second, the serialization and deserialization major role

1. Byte saved (persisted) to a hard disk, are generally written to a file;

2. The sequence of bytes transferred objects on the network;

Third, to achieve serialization and deserialization prerequisite

Object serialization of a class in order to succeed, two conditions must be met:

1. implement java.io.Serializable
all the attributes of the class 2 must be serializable. If a property is not serializable, the property must be marked transient ( transient ).

Fourth, how to implement serialization and de-serialization

ObjectInputStream ObjectOutputStream class and high-level data stream, which comprises a method and deserialize serialized objects.
1.ObjectOutputStream class: using writeObject (Object x) a method for serialization .

public final void writeObject(Object x) throws IOException

2.ObjectInputStream categories: using Object readObject () method deserialization .

public final Object readObject() throws IOException, ClassNotFoundException

Fifth, code implementation

Employees entity class (Employee.java)

 1 package com.cdh;
 2 
 3 /**
 4  * @author chudonghai
 5  * @editor
 6  * @date 2019年9月23日 下午2:16:27
 7  * @version v1.0
 8  * @since 1.0
 9  * @ClassName Employee
10  * @Description 雇员类
11  */
12 public class Employee implements java.io.Serializable
13 
14 {
15     /**
16      * generated serial version ID
17      */
18 is      Private  static  Final  Long serialVersionUID = -4308926516970571240L ;
 . 19  
20 is      public String name;
 21 is      public String address;
 22 is      public  transient  int an SSN;
 23 is      public  int Number;
 24  
25      / ** 
26 is       * Description: The purpose of this method is to write, purely for let class more complete and no other purpose. Do not tangled.
27       * * / 
28      public  void MailCheck () {
 29          System.out.println ( "Check to Mailing A" + name + "" + address);
 30      }
31 }

Serialization achieve Demo:

1  Package Penalty for com.cdh;
 2  / ** 
3  * @author chudonghai
 4  * @editor
 5  * @date 2019 Nian 9 23 afternoon 2:29:01
 6  * @version  
7  * @Since  
8  * @ClassName SerializationDemo
 9  * @Description serialization achieved Demo
 10  * / 
. 11  Import the java.io. * ;
 12 is  
13 is  public  class SerializationDemo
 14  {
 15     public  static  void main (String [] args)
 16     {
 . 17       Employee e = new Employee();
18       e.name = "chudonghai";
19       e.address = "beijing";
20       e.SSN = 999999999;
21       e.number = 101;
22       try
23       {
24          FileOutputStream fileOut =
25          new FileOutputStream("E://aaa//employee.ser");
26          ObjectOutputStream out = new ObjectOutputStream(fileOut);
27          out.writeObject(e);
28          out.close();
29          fileOut.close();
30          System.out.printf("Serialized data is saved in E:/aaa/employee.ser");
31       }catch(IOException i)
32       {
33           i.printStackTrace();
34       }
35    }
36 }

Deserialize achieve Demo:

1  Package Penalty for com.cdh;
 2  / ** 
3  * @author chudonghai
 4  * @editor 
 5  * @date 2019 Nian 9 23 afternoon 2:29:58
 6  * @version  
7  * @Since  
8  * @ClassName DeserializationDemo
 9  * @Description deserialized achieve Demo
 10  * / 
. 11  Import the java.io. * ;
 12 is  
13 is  public  class DeserializationDemo
 14  {
 15     public  static  void main (String [] args)
 16     {
17       Employee e = null;
18       try
19       {
20          FileInputStream fileIn = new FileInputStream("E://aaa//employee.ser");
21          ObjectInputStream in = new ObjectInputStream(fileIn);
22          e = (Employee) in.readObject();
23          in.close();
24          fileIn.close();
25       }catch(IOException i)
26       {
27          i.printStackTrace();
28          return;
29       }catch(ClassNotFoundException c)
30       {
31          System.out.println("Employee class not found");
32          c.printStackTrace();
33          return;
34       }
35       System.out.println("Deserialized Employee...");
36       System.out.println("Name: " + e.name);
37       System.out.println("Address: " + e.address);
38       System.out.println("SSN: " + e.SSN);
39       System.out.println("Number: " + e.number);
40     }
41 }

important point

DETAILED objects need to convert readObject used.

Deserialization, if the sequence of the previous generation can not find the file, it will be reported ClassNotFound.

 

Guess you like

Origin www.cnblogs.com/chudonghai/p/11573880.html