publicclassBlip3implementsExternalizable{ privateint i; private String s; // No initialization publicBlip3(){ print("Blip3 Constructor"); // s, i not initialized } publicBlip3(String x, int a){ print("Blip3(String x, int a)"); s = x; i = a; // s & i initialized only in non-default constructor. }
public String toString(){ return s + i; } publicvoidwriteExternal(ObjectOutput out)throws IOException { print("Blip3.writeExternal"); // You must do this: out.writeObject(s); out.writeInt(i); }
publicvoidreadExternal(ObjectInput in)throws IOException, ClassNotFoundException { print("Blip3.readExternal"); // You must do this: s = (String)in.readObject(); i = in.readInt(); }
publicstaticvoidmain(String[] args)throws IOException, ClassNotFoundException { print("Constructing objects:"); Blip3 b3 = new Blip3("A String ", 47); print(b3); ObjectOutputStream o = new ObjectOutputStream( new FileOutputStream("Blip3.out")); print("Saving object:"); o.writeObject(b3); o.close(); // Now get it back: ObjectInputStream in = new ObjectInputStream( new FileInputStream("Blip3.out")); print("Recovering b3:"); b3 = (Blip3)in.readObject(); print(b3); } }
结果为:
1 2 3 4 5 6 7 8 9 10 11
/* Output: Constructing objects: Blip3(String x, int a) A String 47 Saving object: Blip3.writeExternal Recovering b3: Blip3 Constructor Blip3.readExternal A String 47 *///:~
字段s和i只在第二个构造器中初始化,而不是在默认的构造器中初始化。这意味着假如不在readExternal()中初始化s和i,s就会为null,而i就会为0。如果注释掉跟随于”You must do this“后面的两行代码,当对象被还原后,s是null,而i为0。
publicclassLogonimplementsSerializable{ private Date date = new Date(); private String username; privatetransient String password; publicLogon(String name, String pwd){ username = name; password = pwd; } public String toString(){ return"logon info: \n username: " + username + "\n date: " + date + "\n password: " + password; } publicstaticvoidmain(String[] args)throws Exception { Logon a = new Logon("Hulk", "myLittlePony"); print("logon a = " + a); ObjectOutputStream o = new ObjectOutputStream( new FileOutputStream("Logon.out")); o.writeObject(a); o.close(); TimeUnit.SECONDS.sleep(1); // Delay // Now get them back: ObjectInputStream in = new ObjectInputStream( new FileInputStream("Logon.out")); print("Recovering object at " + new Date()); a = (Logon)in.readObject(); print("logon a = " + a); } }
结果为:
1 2 3 4 5 6 7 8 9 10 11
/* Output: (Sample) logon a = logon info: username: Hulk date: Sat Nov 19 15:03:26 MST 2005 password: myLittlePony Recovering object at Sat Nov 19 15:03:28 MST 2005 logon a = logon info: username: Hulk date: Sat Nov 19 15:03:26 MST 2005 password: null *///:~
publicstaticvoidmain(String[] args) throws IOException, ClassNotFoundException { SerialCtl sc = new SerialCtl("Test1", "Test2"); System.out.println("Before:\n" + sc); ByteArrayOutputStream buf= new ByteArrayOutputStream(); ObjectOutputStream o = new ObjectOutputStream(buf); o.writeObject(sc); // Now get it back: ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream(buf.toByteArray())); SerialCtl sc2 = (SerialCtl)in.readObject(); System.out.println("After:\n" + sc2); } }
结果为:
1 2 3 4 5 6 7 8
/* Output: Before: Not Transient: Test1 Transient: Test2 After: Not Transient: Test1 Transient: Test2 *///:~