I’m a DB idiot so the comment of Cas might as well be written in <fill in the name of some dialect that hasn’t been spoke in a thousand years>.
One potential advantage of interchange style formats can be evolution of the file format. So not version N vs. N+1, but you’re at N and working on what will be in N+1. I don’t find so, but they do have some merit here.
One possible style simply adapting what you already have would be something like this (in-post, probably doesn’t even compile).
public class SaveGame {
DataInputStream src;
int version;
Game game;
// for sanity checking and debugging
void checkTag(int expected, String error) throws Exception
{
int tag = src.readInt();
if (tag == expected)
return;
throw new Exception("whatever" + error); // whatever exception type
}
public static final int HEADER = 0x00000000; // whatever
public static void read(Game game, String filename) throws Exception
{
File load = new File(filename);
SaveGame sg = new SaveGame();
if (load.isFile()) {
sg.src = new DataInputStream(new BufferedInputStream(new FileInputStream(load)));
sg.checkTag(HEADER, "whatever");
sg.version = sg.src.readInt();
sg.game = game;
Game.read(sg);
}
}
// mock classes
public static class Game {
public static void read(SaveGame sg) throws Exception
{
Player.read(sg);
Entity.read(sg);
// whatever else in version 1
if (sg.version > 1) {
// read in all extra stuff in version 2, etc
}
}
}
public static class Player {
private static final int TAG = 0x00000000; // whatever
public static void read(SaveGame sg) throws Exception
{
sg.checkTag(TAG, "player");
// read in all stuff in version 1
if (sg.version > 1) {
// read in all extra stuff in version 2, etc
}
}
}
public static class Entity {
private static final int TAG = 0x00000000; // whatever
public static void read(SaveGame sg) throws Exception
{
sg.checkTag(TAG, "entity");
// read in all stuff in version 1
if (sg.version > 1) {
// read in all extra stuff in version 2, etc
}
}
}
}