user1856971
user1856971

Reputation: 15

Save game to file

I'm working on a primitive RPG, and this class (supposedly) contains all the necessary data:

public class CPU implements Serializable{

private Map<String, Location> locations;
private Map<String, Location> places;
private Map<String, NPC> npcs;
private Game game;
private Player player;
private NPC currentNPC;

public CPU(){

}

(I didn't include the methods, but I think those are irrelevant right now...)

The class "Game" also contains the Player and the CPU as variables, but its constructor isn't the one to actually create them (those are created in the main() method, then added to the classes). This method is the one that's supposed to save the CPU class to a file, so that I can read all the data from it later:

public void SaveGame(String s){
    String sav = s;
    sav.concat(".dat");
    try {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(sav));
        oos.writeObject(cpu);
        oos.close();
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

And this is the method to load it from the file:

public void Load(String s){
    if(s.contains(".dat")){
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(s));
            cpu = (CPU)ois.readObject();
            ois.close();
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

My question is basically: will this work? Am I able to simply serialize the CPU class and save it to a file, then read it back and be able to recover all the data from it (i.e the Player data)?

If I remember correctly, in Java "=" doesn't mean that the object on the right side will be copied, so my other problem is: when the method "Load" finishes, will the "cpu" (variable of the "Game"-class) still contain the CPU that I loaded from the file, and will I be able to read data from it?

Upvotes: 1

Views: 8511

Answers (2)

JB Nizet
JB Nizet

Reputation: 691685

Actually, it will work only if the original file name that you pass to the SaveGame() method contains ".dat".

Indeed, the reading method checks that condition, and the SaveGame() method (which should be named saveGame() to respect the Java naming conventions) doesn't append .dat to the file name as you think. Indeed, Strings are immutable, and the concat() method returns a new String, but doesn't modify the String it's called on. The code should be

String sav = s.concat(".dat");

You should also stop ignoring exceptions like you're doing, and you should always close the streams in a finally block. If you're under Java 7, use the try-with-resources construct.

Upvotes: 1

FThompson
FThompson

Reputation: 28687

If your program has permissions to write files, and all of the field classes (NPC, Game, Player. Location) are serializable as well, then it will work.

The cpu field of the Game class will contain the CPU loaded from the file, if no exception is thrown when reading the object.

Upvotes: 0

Related Questions