如何在正确的位置以降序将highscore追加到highscore.txt文件中?该方法使用先前创建的readHighScore(int score)方法来查找记录应插入的行号。这是我走到这一步:如何在java中追加文件
public static void recordHighScore(String name, int score){
File file = new File("Test/highscore.txt");
FileWriter fwriter = new FileWriter(file, true);
try{
String userName = name+","+score;
fwriter.add(readHighScore(score), userName);
}catch(IOException io){
System.out.println("Missing File!");
}
/*Writes the high score into highscore.txt file, at the correct position (in descending order). This
method uses readHighScore() method to find the line number where the record should be
inserted.*/
}
要附加到文件的中间文件或结束了吗? –
我认为它应该是前10名,所以也许文件的结尾?我不确定。 – zk9
如果您不需要文件顶部的旧信息,那么将数据读入内存,更改内存中的信息并再次保存会很容易。 –