序列化 是将对象的完整信息保存起来的过程(持久化)。    序列化流:ObjectOutputStream

反序列化 是将对象进行还原的过程(反持久化)。               反序列化流:ObjectInputStream

 

 

 

1.序列化

    ①一个对象想要被序列化,这个对象对应的类必须实现Serializable。Serializable接口中没有任何的方法和属性,仅仅用于标志这个类产生的对象可以被序列化。

    ②被static/transient关键字修饰的属性不会被序列化

    ③一个类在实现Serializable接口之后会默认添加一个属性serialVersionUID(版本号),默认是用private static final long 修饰,版本号的值会根据当前类中的属性和方法自动计算。一个对象在序列化的时候会把这个版本号一起序列化出去,反序列化的时候就会比较对象中的版本号和当前类的版本号是否一致---需要手动指定版本号,防止类产生微小改动的时候已经序列化的对象反序列化不回来。

    ④集合和大部分映射不能被序列化。

 1 import java.io.Serializable;
 2 
 3 public class Person implements Serializable {
 4     private static final long serialVersionUID = 1984254532934148317L;
 5 
 6     private String name;
 7     private int age;
 8     private char gender;
 9     private double weight;
10     private transient String cloth; // transient关键字标记的成员变量不参与序列化过程
11     public static String classroom;
12 
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public int getAge() {
22         return age;
23     }
24 
25     public void setAge(int age) {
26         this.age = age;
27     }
28 
29     public String getCloth() {
30         return cloth;
31     }
32 
33     public void setCloth(String cloth) {
34         this.cloth = cloth;
35     }
36 
37     public char getGender() {
38         return gender;
39     }
40 
41     public void setGender(char gender) {
42         this.gender = gender;
43     }
44 
45     public double getWeight() {
46         return weight;
47     }
48 
49     public void setWeight(double weight) {
50         this.weight = weight;
51     }
52 }
Person implements Serializable

相关文章: