defaultReadObject()
调用默认的反序列化机制,并在定义
readObject()
你的方法
Serializable
上课。换句话说,当您具有自定义反序列化逻辑时,您仍然可以返回到默认序列化,这将反序列化您的非静态、非瞬态字段。例如:
public class SomeClass implements Serializable {
private String fld1;
private int fld2;
private transient String fld3;
private void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject(); //fills fld1 and fld2;
fld3 = Configuration.getFooConfigValue();
}
]
另一方面,
在创建
ObjectInputStream
,从反序列化对象外部读取,并希望读取以前序列化的对象:
ObojectInputStream stream = new ObjectInputStream(aStreamWithASerializedObject);
Object foo = (Foo) stream.readObject();