memento备忘录:记录状态 便于回滚 存盘等。
使用的是序列化。
就是将数据序列化,存储在硬盘中。
类想序列化,必须实现标记型接口serializable
然后 使用ObjectOutputStream将对象写入硬盘中。
transient 不序列化该字段
例如:存盘
File f = new File(“path”);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.wirteObject(object1);//参数是需要写出去的对象
oos.wirteObject(object2);//参数是需要写出去的对象
从盘中load:
File f = new File(“path”);
ObjectInputStream oos = new ObjectInputStream (new FileInputStream(f));
object1 = oos.readObject();//返回是需要的对象
object2= oos.readObject();//返回是需要的对象