某个字段不是持久状态的一部分,不应该把字段和对象一起串起来。表示一个域不是该对象串行化的一部分。当一个对象被串行化的时候,transient型变量的值不包括在串行化的表示中,然而非transient型的变量是被包括进去的。输入输出就是一种串行化。
public class TestModel implements Serializable {
private String a;
private transient String b;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String toString() {
return "a = " + a + ";b = " + b;
}
}
public class Test {
public static void main(String[] args) {
TestModel a = new TestModel();
a.setA("a");
a.setB("b");
System.out.println(a.toString());
try {
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("logInfo.out"));
o.writeObject(a);
o.close();
} catch(Exception e) {
}
try {
ObjectInputStream in =new ObjectInputStream(new FileInputStream("logInfo.out"));
TestModel test = (TestModel)in.readObject();
System.out.println(test.toString());
}
catch(Exception e) {
}
}
}