序列化注意事项

参考《深入理解Java web 技术内幕》

1.当父类实现Serializable接口时,所有子类都可以被序列化

父类代码:

import java.io.Serializable;

public class SerialFather implements Serializable {

    private static final long serialVersionUID = -8234435401007920773L;

    private String fatherAttribute = "I am father!";

    @Override
    public String toString() {
        return "SerialFather{" +
                "fatherAttribute='" + fatherAttribute + '\'' +
                '}';
    }
}

子类代码:

public class SerialSon extends SerialFather {

    private String sonAttribute = "I am son!";

    @Override
    public String toString() {
        return "SerialSon{" +
                "sonAttribute='" + sonAttribute + '\'' +
                '}';
    }
}

测试代码:

import java.io.*;

public class Test {

    public static void serial(Object son) {
        ObjectOutputStream oo = null;
        try {
            oo = new ObjectOutputStream(new FileOutputStream(
                    new File("E:/SerialSon.txt")));
            oo.writeObject(son);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static Object deSerial() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(
                    new File("E:/SerialSon.txt")));
            return ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        SerialSon son = new SerialSon();
        //序列化过程
        serial(son);

        //反序列化过程
        Object son2 = deSerial();
        System.out.println(son2);
    }
}

结果:

SerialSon{sonAttribute='I am son!'}

2.子类实现Serializable接口,父类没有,父类中的属性不能被序列化(不报错,但是数据会丢失),子类中的属性仍能正确序列化

 

3.如果序列化的属性是对象,则这个对象也必须实现Serializable接口,否则会报错

4.在反序列化时,如果对象的属性有修改或删减,则修改的部分属性会丢失,但是不会报错

5.在反序列化时,如果serialVersionUID被修改,则反序列化时会失败

6.序列化不保存静态属性

首先,我们new一个对象,然后将该对象序列化后存在文件中

public class SerialFather implements Serializable {

  private static final long serialVersionUID = -8234435401007920773L;

  private static String fatherAttribute = "I am father!";

  @Override
  public String toString() {
    return "SerialFather{" +
        "fatherAttribute='" + fatherAttribute + '\'' +
        '}';
  }
  public static void main(String[] args) throws IOException, ClassNotFoundException {
    SerialFather father = new SerialFather();
    ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("test"));
    oo.writeObject(father);
  }
}

接着,我们改变类中静态属性的值

public class SerialFather implements Serializable {

  private static final long serialVersionUID = -8234435401007920773L;

  private static String fatherAttribute = "I am son!";

  @Override
  public String toString() {
    return "SerialFather{" +
        "fatherAttribute='" + fatherAttribute + '\'' +
        '}';
  }
}

然后反序列化:

  public static void main(String[] args) throws IOException, ClassNotFoundException {
    ObjectInputStream oi = new ObjectInputStream(new FileInputStream("test"));
    SerialFather father1 =  (SerialFather)oi.readObject();
    System.out.println(father1);
  }

结果:

SerialFather{fatherAttribute='I am son!'}

你可能感兴趣的:(Java编程)