【发布时间】:2015-07-14 08:17:19
【问题描述】:
您好,我正在开发一款游戏,一旦我的保存按钮被点击,它就会保存多个状态,例如:姓名、totalScore ...
这就是我保存它的方式:
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);
}
我想加载一个游戏而不是每次都开始一个新游戏。我可以从文本文件中读取数据并将其打印到屏幕上,但不知道如何将这些数据加载到标签中(例如名称)以开始游戏。有什么想法吗?
到目前为止,我加载文本文件的代码是:
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();
}
我在单击“加载”按钮时调用此方法。任何想法将不胜感激。
【问题讨论】:
-
阅读this。