0

I am working on a game and I want to store the High score in a file and read it whenever it's needed. I wanted to use BufferedWriter and BufferedReader like this on the gameover class.

        File fi = new File("score.txt");
        BufferedWriter w = new BufferedWriter(new FileWriter(fi));
        BufferedReader r = new BufferedReader(new FileReader(fi));

So whenever I get a score I will compare it to the score in the file and if the score is greater the score of the file I will store it to the file as a high score so the new high score will be updated in the file. But the problem is every time I run the program the score.txt got the null value, means it's not storing the previous value, it's just get reset every time. Maybe because I am using new FileWriter? I don't know how to do it.

If I use (fi, true), the score is storing like this - 04060100, that means the first line isn't going anywhere, all of the scores are writing in the first line only but I need to store the score in the first line only so that I can read the first line only, high score can't be multiple right?

What to do? I am new to this file storing system in Java.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • If you don't want the file to be truncated when you open a file for writing, open it in "append" mode. – Stephen C Apr 30 '22 at 06:59
  • By the way you are not writing null to the file. You are ... in effect truncating ... by making it empty. Null is a specific value ... of some data types ... in some programming languages. It has no meaning in the context of files / file systems. And you don't "assign" to a file. – Stephen C Apr 30 '22 at 07:03
  • But if I append a new text in the existing file it looks like this- 00102030 means the first line is not really the previous number but it’s just garbage, I need to use only one number. So I want to make a new text in the place of the existing text. Means after I play the game sometimes the file will look like this - 100 (the highscore only) – Newbie_programmer Apr 30 '22 at 07:05
  • OK ... so I don't understand what you are asking. If you don't want to truncate the file, and you don't want to append to it, why are you even opening it to write? Do you want to update the first line or a text file and keep the rest of the file the same. In that case, you need to rewrite the entire file ... each time you update it. – Stephen C Apr 30 '22 at 07:07
  • Okay I will try to make you understand. Look, I want to store the highscore only to that file. So at first when the program runs, the score will be 0 right? So the score.txt file will have 0 in the first line. Okay? After playing a game let's say I get a score of 50, so obviously 50 is greater than 0, then the score.txt file will have the 50 in the first line not 0. Now again I played the game and get a score of 30, which is not greater than 50 so the txt file will be the same, it contains the high score in the first line which is 50. When I scored 100 the txt will store the 100. Get it? – Newbie_programmer Apr 30 '22 at 07:20

1 Answers1

0

Okay I will try to make you understand. Look, I want to store the highscore only to that file. So at first when the program runs, the score will be 0 right? So the score.txt file will have 0 in the first line. Okay? After playing a game let's say I get a score of 50, so obviously 50 is greater than 0, then the score.txt file will have the 50 in the first line not 0. Now again I played the game and get a score of 30, which is not greater than 50 so the txt file will be the same, it contains the high score in the first line which is 50. When I scored 100 the txt will store the 100. Get it?

Yes ... that is a much clearer explanation of what you are trying to do than what you wrote in your original question. (If only you have explained it properly in the first instant ...)

The solution is to NOT open the file for writing until you know that it needs to be written. The logic should be:

  1. Open file to read
  2. Read line containing high score
  3. Close
  4. If current score greater than high score
    1. Open file to write
    2. Write new high score
    3. Close.

What your code currently does is to open for writing before you even read the file. That truncates the file, so that when you try to read the current high score the information has already been trashed.

Note that it is not the BufferedWriter or BufferedReader that is the problem. The damage is done by calling the FileWriter constructor. (The javadoc should explain it.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank you so much mate! You really saved me there. Now it worked. I really love this community. I just used a condition to specify when this filewriter should be called. Thanks again good sir. – Newbie_programmer Apr 30 '22 at 07:44