序列化注意事项

参考《深入理解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!'}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值