1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public class TestSerializable implements Serializable {
private transient List<String> s = new ArrayList<String>();
public TestSerializable() {
s.add("abc");
s.add("def");
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
s = new ArrayList<String>();
s.add("abc");
s.add("def");
}
public static void main(String...args) throws Exception {
TestSerializable ser = new TestSerializable();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("test.dat")));
try {
oos.writeObject(ser);
} finally {
oos.close();
}
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("test.dat")));
TestSerializable res = null;
try {
res = (TestSerializable)ois.readObject();
} finally {
ois.close();
}
System.out.println(res.s);
}
} |