Just an added extra point onto the previous answer.
If you wish to have a more complicated amount of info (for example, saving a players inventory) then the XML format would be difficult. The way you could do it is to make a class called something like “PlayData” and then save it as an object. Like so:
public void saveData(PlayData pd) throws Exception{
//The file it saves to
File filePath ="saves";
if(!filePath.exists()){
//log.info("File path does not exist, creating it...");
filePath.mkdirs();
}//log.info("Got file path, attempting to get file...");
File file = new File("saves/player.data");
if(!file.exists()){
file.createNewFile();
}
FileOutputStream save = new FileOutputStream(file);
// Write object with ObjectOutputStream
ObjectOutputStream saveOut = new ObjectOutputStream (save);
//Now write it out
SaveOut.writeObject (pd);
}
And then to load it:
public PlayerData getPlayerData() throws Exception{
File filePath =new File("saves/player.data");
if(!filePath.exists()){
return null;
}
FileInputStream save = new FileInputStream(filePath);
// Read object using ObjectInputStream
ObjectInputStream saveIn = new ObjectInputStream (save);
// Read an object
Object obj = saveIn.readObject();
if (obj instanceof PlayerData){
return (PlayerData) obj;
}else{
return null;
}
}
So long as everything inside the PlayerData class (including itself) implements Serializable, then this will work.
Hope I helped!