Hi am working on a game which saves several states once my save button is cliked, such as: name, totalScore ...
This is how I saved it:
public static void save()
{
fileName = new File("Saved Game");
try
{
fw = new FileWriter(fileName,true);
}
catch (IOException e)
{
e.printStackTrace();
JOptionPane.showConfirmDialog(frame, e.toString() + "\nFail to save game.",
"Saved", JOptionPane.DEFAULT_OPTION);
}
f = new Formatter(fw);
f.format("\n%s\t",level);
f.format("\t%s\t", nameLabel.getText());
f.format("\t%s\t\t", updatedLabel.getText());
f.close();
JOptionPane.showConfirmDialog(frame, "Saving was successful.", "Game Saved",
JOptionPane.DEFAULT_OPTION);
}
I want to load a game instead of starting a new one every time. I can read the data from a text file and print it to screen, but don't know how to load these into the labels (e.g. for name) to begin the game. Any ideas?
My code for loading the text file so far is:
public static void readFromFile (String fileName) throws IOException
{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
System.out.println(br.readLine());
br.close();
}
I call this method at a "load" button click. Any ideas would be greatly appreciated.