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.
If your program needs to save state, you can do it the hard way, interrogating each object, then painstakingly writing the value of each instance variable to a file, in a format you create.
Make the changes to the contents of the Java file. When you have finished working, click File > Save or File > Save All to save the file and retain all your changes.
Saving object in java can be accomplished in a number of ways, the most prominent of which is to serialize the object - saving it to a file and reading the object using an ObjectOutputStream and ObjectInputStream, respectively. Serialization is a fantastic advantage to using java.
This process of restoring the object's old state is known as Deserialization.
There is no real way to "store the state of the whole JVM" as such.
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 the Java Object Serialization Specification.
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 ...
Most probably you just want to store some state 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.
http://download.oracle.com/javase/7/docs/api/java/io/package-summary.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With