我一直在尝试在实现特殊序列化(甚至序列化瞬态对象)时弄清楚流程,但我无法理解流程:
public class object1 implements Serializable {
int i = 2032423;
String str = "dssadsadsdfsfsdfczxc";
StringBuilder sb = new StringBuilder();
transient testobject ob1 = new testobject();
String str2;
testobject ob2;
String sooo =new String("jbdskdbshxcbc");
public static void main(String[] args) throws ClassNotFoundException {
try {
FileOutputStream fos = new FileOutputStream(new File(
"serialTst.txt"));
ObjectOutputStream oos = new ObjectOutputStream(fos);
object1 obj1 = new object1();
obj1.ob1.str = "this guy is referred";
oos.writeObject(obj1);
oos.flush();
oos.close();
fos.close();
FileInputStream fis = new FileInputStream("serialTst.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
object1 obb=(object1)ois.readObject();
System.out.println(obb.str2);
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void readObject(ObjectInputStream Aois)
throws ClassNotFoundException, IOException {
Aois.defaultReadObject();
str2 = (String) Aois.readObject();
System.out.println(ob1.str);
System.out.println(sooo); // Why Null here??
}
private void writeObject(ObjectOutputStream Aoos) throws IOException {
Aoos.defaultWriteObject();
Aoos.writeObject(ob1.str);
}
}
** 为什么 String sooo 为 null,即使打印正常 (String sooo="something") ????**
如果没有创建 object1 类的实例,那么 readObject 和 writeObject 是如何执行的?