How can I save the state of my program and then load it?

There are any number of possible choices…

You could

Use the Properties API, which provides save and load functionality.

The API works like a Map, allowing you to store key/value pairs, which you can save and load as required.

The API only allows you to store String values, so you would need to convert non-String values manually.

Just remember to save them as the API does not automatically persist the changes

Take a look at Properties for more details.

You could

Roll you own XML file or use something like JAXB which allows you to bind properties of object and export/import them to/from XML

This approach would be more flexible then using Properties, but introduces a level of complexity

You Could

Use the Preferences API, which allows you to store String and primitive values, with out the need to perform any type of converstion.

The Preferences API also automatically loads and stores it’s content, but it will do so where it wants to, so you lose control over where the content is stored.

You Could

Use a stand alone/single user database like H2 or HSQLDB for example. It’s a little more complicated but does take care of the basic storage requirements.

It would also require additional time to update should you change your requirements, over something like using Properties or Preferences and might be a little overkill if all you are storing is the cell data…IMHO

You Could

Try using object serialization, but the API was never meant for the long term storage of object states and carrie it’s bag of problems and I would personally avoid it, but that’s me.

Leave a Comment