Java

文档:
 http://www.runoob.com/java/java-serialization.html
 http://www.importnew.com/24490.html
 https://www.cnblogs.com/wxgblogs/p/5849951.html

概述:
 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据、有关对象的类型的信息和存储在对象中数据的类型;
 将序列化对象写入文件之后,可以从文件中读取出来,并且对它进行反序列化,也就是说,对象的类型信息、对象的数据,还有对象中的数据类型可以用来在内存中新建对象;
 整个过程都是 Java 虚拟机(JVM)独立的,也就是说,在一个平台上序列化的对象可以在另一个完全不同的平台上反序列化该对象;
 类 ObjectInputStream 和 ObjectOutputStream 是高层次的数据流,它们包含序列化和反序列化对象的方法;
 当序列化一个对象到文件时,按照 Java 的标准约定是给文件一个.ser扩展名;

一个类的对象要想序列化成功,必须满足两个条件:
 该类必须实现 java.io.Serializable 对象;
 该类的所有属性必须是可序列化的;如果有一个属性不是可序列化的,则该属性必须注明是短暂的;
 注意:如果你想知道一个 Java 标准类是否是可序列化的,请查看该类的文档;检验一个类的实例是否能序列化十分简单, 只需要查看该类有没有实现 java.io.Serializable接口;

 1 /**
 2  * 实现了 java.io.Serializable 接口的类
 3  */
 4 package serialize;
 5 
 6 public class Employee implements java.io.Serializable
 7 {
 8     private static final long serialVersionUID = 1L;
 9     
10     private String name;
11     private String address;
12     private transient int SSN;
13     private int number;
14     
15     public void mailCheck()
16     {
17        System.out.println("Mailing a check to " + name + " " + address);
18     }
19 
20     public String getName()
21     {
22         return name;
23     }
24     public void setName(String name)
25     {
26         this.name = name;
27     }
28     public String getAddress()
29     {
30         return address;
31     }
32     public void setAddress(String address)
33     {
34         this.address = address;
35     }
36     public int getSSN()
37     {
38         return SSN;
39     }
40     public void setSSN(int sSN)
41     {
42         SSN = sSN;
43     }
44     public int getNumber()
45     {
46         return number;
47     }
48     public void setNumber(int number)
49     {
50         this.number = number;
51     }
52 }
实现了 java.io.Serializable 接口的类
 1 /**
 2  * ObjectOutputStream 类的使用:序列化一个对象
 3  */
 4 package serialize;
 5 
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.ObjectOutputStream;
 9 
10 public class SerializeTest01
11 {
12     public static void main(String[] args)
13     {
14         Employee employee = new Employee();
15         employee.setAddress("地址");
16         employee.setName("姓名");
17         employee.setNumber(01);
18         employee.setSSN(0001);
19         
20         FileOutputStream fileOut = null;
21         ObjectOutputStream out = null;
22         try
23         {
24             fileOut = new FileOutputStream("F://test//employee.ser");
25             out = new ObjectOutputStream(fileOut);
26             out.writeObject(employee);
27             System.out.printf("Serialized data is saved in F:/test/employee.ser");
28         }
29         catch(IOException i)
30         {
31             i.printStackTrace();
32         }
33         finally
34         {
35             if(null != out)
36             {
37                 try
38                 {
39                     out.close();
40                 } 
41                 catch (IOException e)
42                 {
43                     e.printStackTrace();
44                 }
45             }
46             
47             if(null != fileOut)
48             {
49                 try
50                 {
51                     fileOut.close();
52                 } 
53                 catch (IOException e)
54                 {
55                     e.printStackTrace();
56                 }
57             }
58             
59         }
60         
61     }
62 }
ObjectOutputStream 类的使用:序列化一个对象
 1 /**
 2  * ObjectInputStream 类的使用:反序列化生成对象
 3  */
 4 package serialize;
 5 
 6 import java.io.FileInputStream;
 7 import java.io.IOException;
 8 import java.io.ObjectInputStream;
 9 
10 public class SerializeTest02
11 {
12     public static void main(String[] args)
13     {
14         Employee e = null;
15         
16         FileInputStream fileIn = null;
17         ObjectInputStream in = null;
18         
19         try
20         {
21             fileIn = new FileInputStream("F://test//employee.ser");
22             in = new ObjectInputStream(fileIn);
23             e = (Employee) in.readObject();
24         }
25         catch(IOException i)
26         {
27             i.printStackTrace();
28             return;
29         }
30         catch(ClassNotFoundException c)
31         {
32             System.out.println("Employee class not found");
33             c.printStackTrace();
34             return;
35         }
36         finally
37         {
38             if(null != fileIn)
39             {
40                 try
41                 {
42                     fileIn.close();
43                 } 
44                 catch (IOException e1)
45                 {
46                     e1.printStackTrace();
47                 }
48             }
49             if(null != in)
50             {
51                 try
52                 {
53                     in.close();
54                 } 
55                 catch (IOException e1)
56                 {
57                     e1.printStackTrace();
58                 }
59             }
60         }
61         
62         System.out.println("Deserialized Employee...");
63         System.out.println("Name: " + e.getName());
64         System.out.println("Address: " + e.getAddress());
65         // 注意:当对象被序列化时,属性 SSN 的值为 0001,但是因为该属性是短暂的,该值没有被发送到输出流;
66         // 所以反序列化后 Employee 对象的 SSN 属性为 0
67         System.out.println("SSN: " + e.getSSN());
68         System.out.println("Number: " + e.getNumber());
69         
70         e.mailCheck();
71     }
72 }
ObjectInputStream 类的使用:反序列化生成对象
原文地址:https://www.cnblogs.com/kehuaihan/p/8463887.html