public class TransientTest implements Serializable{
private static final long serialVersionUID = -2670851086407643335L;
private transient int a = 1; //不会被序列化(持久化),将该对象保存到磁盘中的时候,该属性不会被保存
private int b = 8;
public int getA() {
return a;
}
public int getB() {
return b;
}
}
//测试类
public class RunTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//write Serializable object to file
TransientTest test = new TransientTest();
FileOutputStream fileOutputStream = new FileOutputStream("KeyWords/res/transienttest.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(test);
objectOutputStream.flush();
objectOutputStream.close();
//get Serializable object from file
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("KeyWords/res/transienttest.txt"));
TransientTest transientTest = (TransientTest) objectInputStream.readObject();
System.out.println("a=" + transientTest.getA() + "\t" +"b=" + transientTest.getB());
}
}