Memento 备忘录
Memento 备忘录
- 记录状态便于回滚
- 记录对象的某一个瞬间(快照)
序列化与反序列化
public class Memento {public static void main(String[] args) {Test test = new Test(1,2,"测试");File file = new File("d:/LZY/test.data");ObjectOutputStream objectOutputStream = null;ObjectInputStream objectInputStream = null;try {objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));objectInputStream = new ObjectInputStream(new FileInputStream(file));objectOutputStream.writeObject(test);Test test1 = (Test) objectInputStream.readObject();System.out.println(test1.toString());} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally{if(objectOutputStream!=null){try {objectOutputStream.close();} catch (IOException e) {e.printStackTrace();}}if (objectInputStream !=null){try {objectInputStream.close();} catch (IOException e) {e.printStackTrace();}}}}}class Test implements Serializable {int a;int b;String str;public Test(int a, int b, String str) {this.a = a;this.b = b;this.str = str;}public String toString(){return "a:"+a+" b:"+b+" str:"+str;}}
