I am making a text based game for my programming class, and am working on the saving system at the moment, however, when I run the save method, it overwrites the file, but doesn't insert the new data, it leaves the file blank.
Here is the whole save class.
package character;
//
// charSaveHandler.java
// main
//
// This file deals with saving the character
//
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
public class charSaveHandler {
/* Create any needed objects */
/* Assign needed variables */
private Formatter saveChar;
//public Scanner openChar;
public charSaveHandler(String charName, int charLevel, int charExp){
try{
File exist = new File("characters/" + charName + ".txt");
if(exist.exists()){
saveChar = new Formatter("characters/" + charName + ".txt");
saveChar.format("%s%s%s", charName, charLevel, charExp);
}
else{
System.out.println("Can not save character. It does not exist.");
}
}
catch(Exception e){
System.out.println("Unable to find character " + charName);
}
}
public void openChar(String charName){
try{
saveChar = new Formatter("characters/" + charName + ".txt");
}
catch(Exception e){
//System.out.println("");
}
}
public void saveToDB(String charName, int charLevel, int charExp){
try{
saveChar.format("%s%s%s", "d", " 50 ", "0");
System.out.println("Character " + charName + " saved!");
}
catch(Exception e){
System.out.println("Cannot save character " + charName);
}
}
}
It also outputs the it has saved the character.