Seems all right to me. There are a few minor issues. Idiomatically one would use a finally block for closing the file streams. A Serializable class should consider having a known serialVersionUID, though this means that you need to be concious when serialized classes become incompatible. You also need to understand that Java will eagerly serialise the entire object graph, thus you need a mechanism for controlling how much of the graph is serialised (using the "transient" keyword) and how to rebuild the full graph from the partially saved graph.
When I used Java in the past for this, I tried to isolate the serialization from the actual objects of interest. That is, I would create an inner serialised class which contained just enough information to re-construct the overall object. This object acted as a kind of "factory", which when passed a "context" object was able to rebuild the full object, including restoring some references that would not be possible to save.
As an example, imagine a simple "game" that consists of little dust motes that spawn at the top of the screen and fade over time. I have just written this code right now, it has neither been compiled nor tested.
Let us imagine we are using some helper classes from other libraries:
// Possibly from some physics or game library
public class Vec2 {
public float x;
public float y;
}
// Possibly from some game or graphics library
public class Texture {
// ...
}
Note that these classes might be provided by a third party and might not be serializable.
Now here is a basic game object class we want to be able to serialize:
public class DustMote {
private static final float InitialLifetime = 60;
private final Vec2 position = new Vec2();
private final Texture texture;
private float life;
public DustMote(Vec2 position, Texture texture) {
this.position.set(position);
this.texture = texture;
this.life = InitialLifetime;
}
// ...
}
You can imagine some simple functionality to decrement the life field as time progresses and to draw the texture in the appropriate position. The actual game logic is irrelevant here.
Let us create a few interfaces to allow us to define what we want to achieve:
public interface GameContext {
public Texture getTexture(String name);
}
public interface Saveable {
public Restorable save();
}
public interface Restorable extends Serializable {
public Saveable restore(GameContext gameContext);
}
The Saveable will be implemented by the game object. The Restorable will be a simple immutable object containing just the fields of interest. The GameContext is the connection that allows us to reconnect the revived objects correctly into the object graph. In this case, it allows the game objects to retrieve other objects, in this case textures.
Here is what our game object might become:
public class DustMote implements Saveable {
private static final float InitialLifetime = 60;
private final Vec2 position = new Vec2();
private final Texture texture;
private float life;
public DustMote(Vec2 position, GameContext context) {
this(position, InitialLifetime, context);
}
private DustMote(Vec2 position, long life, GameContext context) {
this.position.set(position);
this.texture = context.getTexture("dust-mote");
this.life = life;
}
// ...
@Override
public Restorable save() {
return new DustModeState(this);
}
private static class DustMoteState implements Restorable {
private static final long serialVersionUID = 1L;
private final float x;
private final float y;
private final float life;
public DustMoteState(DustMote dustMote) {
this.x = dustMote.position.x;
this.y = dustMote.position.y;
this.life = dustMote.life;
}
@Override
public DustMote restore(GameContext context) {
Vec2 position = new Vec2(x, y);
return new DustMote(position, life, context);
}
}
}
The advantage of this approach is that it separates the serialised state from the current game state. Thus you don't need to weaken the game object's invariants just to support reviving the object. We have also removed the problem of worrying about whether our libraries have taken serialization into account.
When we want to save the game, the game just iterates through each Saveable, calling the save() method - the returned objects can be sent to the ObjectOutputStream. During loading, you can iterate through the ObjectInputStream, casting each returned value to a Restorable and calling the restore() object on it. The returned object would be added to some kind of collection in a "game world" object, or similar.
I'm not saying this is the best way, but it avoids some problems with directly serializing the game objects while being reasonably quick to get up and running, compared to hand writing serialisation routines.