Java序列化进阶

1. Java序列化允许重构

Java对象在序列化后,如果对该对象所属类进行重构,比如新增属性字段,则可用重构后的类把已有流(重构前的类序列化生成的流)中的对象还原出来,还原后生成的对象也拥有重构后新增的属性字段且值为null。相反,重构后生成的序列化数据也可用来还原为重构前的类对象。注意:这一系列操作,要保证重构前后的两个类拥有相同的序列化版本hash即serialVersionUID。参考代码如下:

package serial; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class Person implements Serializable{ /** * serialVersionUID-不可缺少! */ private static final long serialVersionUID = 6017343616551062466L; private String name; private String age; private String weight; private Person father; //重构,添加mother属性(执行firstDemo时注释掉) private Person mother; public Person(String name, String age, String weight){ this.name = name; this.age = age; this.weight = weight; } public static void main(String[] args) { // firstDemo();//一次完整的序列化和反序列化过程,序列化产生一个对象流文件记录对象数据,反序列化用来还原对象 secondDemo();//重构该类后,用流中重构前的对象数据创建重构后的类对象(执行firstDemo时注释掉) } private static void firstDemo(){ Person 张大 = new Person("张大","40","80KG"); Person 张三 = new Person("张三","10","50KG"); 张三.setFather(张大); List family = new ArrayList(); family.add(张大); family.add(张三); try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("relationship.fam")); out.writeObject(family); out.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream("relationship.fam")); List recoveryFamily = (List)in.readObject(); in.close(); for(Person person: recoveryFamily){ System.out.println(person); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private static void secondDemo() { ObjectInputStream in; try { in = new ObjectInputStream(new FileInputStream("relationship.fam")); List recoveryFamily = (List) in.readObject(); in.close(); for (Person person : recoveryFamily) { System.out.println(person); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(String age) { this.age = age; } public String getAge() { return age; } public void setWeight(String weight) { this.weight = weight; } public String getWeight() { return weight; } public void setFather(Person father) { this.father = father; } public Person getFather() { return father; } //重构前 // public String toString(){ // return "name:"+this.name+" age:"+this.age+" weight:"+weight+" father:["+this.father+"]"; // } //重构,添加mother信息(执行firstDemo时注释掉) public String toString(){ return "name:"+this.name + " age:"+this.age + " weight:"+weight + " father:["+this.father + "] mother:["+this.mother+"]"; } //重构时添加的方法(执行firstDemo时注释掉) public void setMother(Person mother) { this.mother = mother; } //重构时添加的方法(执行firstDemo时注释掉) public Person getMother() { return mother; } }

 

 

你可能感兴趣的:(Java)