参考:https://en.wikipedia.org/wiki/Serialization
1.什么是序列化和反序列化
In computer science, in the context of data storage, serialization (or serialisation) is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later (possibly in a different computer environment)
翻译:序列化,就是把数据结构或者对象转换到一种便于存储,传输,以及重reconstructed 的格式的过程
When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object
翻译:当resulting series of bits 被按照序列或的格式重新读取后,这个可以来创建一个最初的对象的语义上完全的clone
点评:突然想到有些科幻小说中,把人粒子化,然后光速传播到另一个地方,再重新reconstructed。感觉有点相似。
For many complex objects, such as those that make extensive use of references, this process is not straightforward.
翻译:对一些复杂的对象,比如有大量应用,这个过程就不那么简单了。
Serialization of object-oriented objects does not include any of their associated methods with which they were previously linked.
翻译:面向对象的对象不包括之前和他们相关联的方法。
This process of serializing an object is also called marshalling an object.[2] The opposite operation, extracting a data structure from a series of bytes, is deserialization (also called unmarshalling).
翻译:序列化过程也叫marshalling an object 。反方向的操作,叫做反序列化(也叫作 unmarshalling)
2.java中如何实现序列化
Java中实现序列化只要实现Serializable接口即可
举例:TestBean类(如果是在android中主要要申请读写权限)
public class TestBean implements Serializable {
public String desc;
public String property1;
public String property2;
@Override
public String toString() {
return "TestBean{" +
"desc='" + desc + '\'' +
", property1='" + property1 + '\'' +
", property2='" + property2 + '\'' +
'}';
}
}
序列化以及反序列化的方法
public void serialization(View view) {
try {
TestBean testBean = new TestBean();
testBean.property1 = "属性1";
testBean.property2 = "属性2";
testBean.desc = "我是testbean";
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("/sdcard/aaatest.txt"));
objectOutputStream.writeObject(testBean);
} catch (IOException e) {
e.printStackTrace();
}
}
public void deserialization(View view) {
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream("/sdcard/aaatest.txt"));
TestBean testBean = (TestBean) objectInputStream.readObject();
Log.i(TAG,"deserialization"+testBean.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
查看aaatest.txt 文件,里面有一些乱码
sr calc.superdy.ttest.test.TestBean`仍旾?C L desct Ljava/lang/String;L property1q ~ L property2q ~ xpt 鎴戞槸testbeant 灞炴€?t 灞炴€?
经过上面的测试,序列化到文件中的对象通过反序列化过程可以正常读取
3.关于serialVersionUID
如果实现序列化了的类中有字段变动,切没有主动申明 serialVersionUID ,则系统会自动生成一个serialVersionUID
所以为了兼容,还是要自己手动申明 serialVersionUID
例如:写的时候是这样的结构
public class TestBean implements Serializable {
public String desc;
public String property1;
public String property2;
// public static final int serialVersionUID = 1;
@Override
public String toString() {
return "TestBean{" +
"desc='" + desc + '\'' +
", property1='" + property1 + '\'' +
", property2='" + property2 + '\'' +
'}';
}
}
读的时候
public class TestBean implements Serializable {
public String desc;
public String property1;
public String property2;
public String property3;
// public static final int serialVersionUID = 1;
@Override
public String toString() {
return "TestBean{" +
"desc='" + desc + '\'' +
", property1='" + property1 + '\'' +
", property2='" + property2 + '\'' +
'}';
}
}
则会报如下错误:
/System.err: java.io.InvalidClassException: calc.superdy.ttest.test.TestBean; local class incompatible: stream classdesc serialVersionUID = 6974057760637855555, local class serialVersionUID = -1037588103870301871