java 保存程序当前状态的Java方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7888380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java method to save current state of a program
提问by Zee
Is it possible to save the current state of a java program and then reload it? The program is fairly complicated. I just need to be able to save the current state to a file and then reload it. Could you please refer me to a java library or a place to read more about that from.
是否可以保存java程序的当前状态然后重新加载它?该程序相当复杂。我只需要能够将当前状态保存到文件中,然后重新加载它。你能不能请我参考一个java库或一个可以阅读更多相关信息的地方。
回答by Joachim Sauer
There is no real way to "store the state of the whole JVM" as such.
没有真正的方法来“存储整个 JVM 的状态”。
But you could encapsulate the relevant state of your application in one or more objects and then serialize those objects. That sounds more complicated than it really is, because most likely the state of your application is already(mostly) encapsulated in some objects.
但是您可以将应用程序的相关状态封装在一个或多个对象中,然后序列化这些对象。这听起来比实际更复杂,因为很可能您的应用程序的状态已经(大部分)封装在某些对象中。
This serialization tutorial provides more information, for more details see this page.
此序列化教程提供了更多信息,有关更多详细信息,请参阅此页面。
回答by Arne Deutsch
The "state" of a java program is a complex beast. This would include the complete heap, the execution point of all threads, values of local variables ...
Java 程序的“状态”是一个复杂的野兽。这将包括完整的堆、所有线程的执行点、局部变量的值......
Most probably you just want to store somestate and later load this in a new program. You could use the classes in java.io
to write state to files and later read them again.
很可能您只想存储一些状态,然后将其加载到新程序中。您可以使用 in 类java.io
将状态写入文件,然后再次读取它们。
http://download.oracle.com/javase/7/docs/api/java/io/package-summary.html
http://download.oracle.com/javase/7/docs/api/java/io/package-summary.html