Reading Objects from disk

Hi!

I often find myself saving maps, scores, and other data to disk by using the ObjectInputStream/ObjectOutputStream classes. I find them quite convinient and very easy to implement.

But a common problem for me is that i change the signature of the classes that I write to disk. I.e I will rewrite parts of class HighScores and thus make the copies kept at disk unreadble by the objectoutputstream even though the data still is mostly the same.

This is currently not a real problem but I can see a problem emerging with this in the future. How do you guys solve this problem?

Serialized objects are generelly used for short time storage.

For highscores, levels and whatsoever you should choose a suited format. Settings for example can be stored nicely with Properties and tile based maps just with a bunch of numbers… that can look like this:


[...]
11113333331111111111 11113333331111111111 11113333331111111111 10000000001000000001 10000000001000000001 10000000001000000001 10000000001000000000 10000000000000000000 10000000000001111111 10000000000001111111 10001111333201111111 10000000000001111111
[...]

XML or a database is also an option, but most likely it’s overkill.

Or give your objects an explicit serialVersionUID and then use versioning to deserialize new versions of the class.

Cas :slight_smile: