问题
Hi so I have an assignment due in 6 hours and I am completely lost. I have new methods added to my existing work done so far but I don't know how to do them. The text file has names and scores, and the first method needs to check if the score is among the top 10 and return true/false:
the last method that I am lost in needs to write the high score into highscore.txt file, at the correct position(in descending order), the method uses readHighScore() method to find the line number where the record should be inserted.
public static void recordHighScore(String name, int score){
}
回答1:
You can't insert data into text files. You can only append/rewrite. These are the steps:
- Read data and store into objects.
- Get the highest/lowest from the objects (you can also sort them with
Collections.sort()
orArrays.sort()
) - Write the sorted objects back into the text file. (You will be overwriting the text file with new data)
来源:https://stackoverflow.com/questions/34241413/java-filing-and-highscore