本文中JDK版本为1.8.0_151
1. Java序列化
Java中,类通过继承Serializable即可实现序列化功能,其中需要需要注意以下几点:
建议显示设置serialVersionUID 值,除非你能确定该类以后不会修改。关于serialVersionUID作用,请自行google
如果需要定制化序列化方法,只需要添加方法writeObject与writeObject
如果忽略某些字段, 加上transient 即可
2. 测试代码
public class Test implements Serializable {
private static final long serialVersionUID = -7200951545614686243L;
private final int a;
public Test(int a) {
this.a = a;
}
public static void main(String[] args) {
try {
Test test = new Test(1);
FileOutputStream fileOutputStream = new FileOutputStream("test.txt", false);
ObjectOutputStream o = new ObjectOutputStream(fileOutputStream);
o.writeObject(test);
FileInputStream fileInputStream = new FileInputStream("t