Store/Restore snapshot/state 整个应用程序

Store/Restore snapshot/state of entire application

我想存储整个应用程序状态,然后在下次启动时恢复它。有没有图书馆可以让我更轻松?或者大家有什么建议吗?

独立应用程序

有一种设计模式可用于您的目的,它就是 Memento 模式。

The memento pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). - Wikipedia

您可以阅读 wiki 页面中提供的示例,了解如何在您的代码中使用它。

如果你想将一个对象的状态保存为一个文件,并且在你的程序执行结束后仍然可用,你应该在你想要存储的 class 中实现 Serializable 接口.

示例:

public class Example implements Serializable
    {

    }

以及实例化 class 的位置:

try{

    Example c = new Example();
    FileOutputStream fout = new FileOutputStream("YOURPATH");
    ObjectOutputStream oos = new ObjectOutputStream(fout);   
    oos.writeObject(c);
    oos.close();
    System.out.println("Done");

   }catch(Exception ex){
       ex.printStackTrace();
   }