.properties alternative?

I want to be able to save a few variables in my game before exiting and have these values loaded into my classes once I start it again.

Through googling I came upon Properties, which seemed to suit my needs well, as it’s seemed simpler than having to write and read from a .txt file.

However, there are a few things I don’t like about it:
-All values are interpreted as strings and I have to cast everything I write and everything I read.

  • it’s still somewhat of a hassle, having to create in/out-streams etc.
  • unless I extend the properties class and override methods, there’s a lot of things I can’t control, like the order of how values are stored, etc.

So, I was wondering if there’s a simpler way of doing what I’m aiming for?

JSON

You can just create a Java class representing your stored values and serialize it by making it Serializable and use standard java serialization:
http://www.tutorialspoint.com/java/java_serialization.htm
http://docs.oracle.com/javase/tutorial/javabeans/advanced/persistence.html
http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html

If you want to have the output readable, you can use XMLEncoder:
http://docs.oracle.com/javase/7/docs/api/java/beans/XMLEncoder.html

If you don’t mind using 3rd party libs, take a look at XSTREAM
http://xstream.codehaus.org/tutorial.html

If you don’t like having xml output, take the JSON route
http://xstream.codehaus.org/json-tutorial.html

Or look for other similar libs like:

Excellent, thanks a million!

By eying through the helpful links I think I’m going to go with JSON for graphic settings and keyboard config, entity-stats and things that people might want to mod in the future. For in-game saving I think I’ll serialize (simple and creates slightly “encrypted” files.) Any objections to this?

If you are using xstream, you can use the BinaryStreamDriver:
http://xstream.codehaus.org/javadoc/com/thoughtworks/xstream/io/binary/BinaryStreamDriver.html
for generating non human-readable output. IMO xstream is very easy to use and quite forgiving when classes change compared to pure java serialization.