我创建了一个Java游戏,当游戏结束时,会执行一个方法告诉用户输入他/她的名字,然后他们的分数将保存在playscores.txt文档中。这工作正常。但是,我想要的不仅仅是这个文档中的一个人的分数。我想要它,所以每个玩游戏名称和分数的人都会被保存在这个文档中。真的很感谢一些帮助。FileWriter,How to write on the same document
这是我gameComplete方法代码:
public void gameComplete() throws IOException {
String name = (String) JOptionPane.showInputDialog(
frame,
"Enter your name: ",
"Save Score",
JOptionPane.PLAIN_MESSAGE);
Score score = new Score(player.getScore(), name);
FileWriter fstream = new FileWriter("playerscores.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Name : " + score.getName() + System.getProperty("line.separator") );
out.write("Score : " + score.getScore());
out.close();
}
我曾尝试不同的东西,比如ObjectOutputStream的可惜无法弄清楚如何做到这一点,并想知道如果它甚至有可能。此外,我想知道我应该使用什么类来完成这件事。
也[这里](http://stackoverflow.com/questions/3005124/writing-to-an-already-existing-file-using-filewriter-java),还[此问题](http:// stackoverflow.com/questions/1616746/java-filewriter-overwrite?rq=1)。 –
@HovercraftFullOfEels我发现我自己的帖子的创建者已经要求将其删除,因为有重复的帖子。 –