如何在每次调用该方法时停止BufferedWriter分配空值?

发布于 2025-01-25 03:08:58 字数 553 浏览 4 评论 0原文

我正在研究游戏,我想将高分存储在文件中,并在需要时阅读它。我想在Gameover类中使用这样的BufferedWriter和BufferedReader。

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

因此,每当我获得分数时,我都会将其与文件中的分数进行比较,如果分数更大,则文件的分数将作为高分子存储到文件中,因此新的高分将在文件中更新。但是问题是每次我运行程序时,score.txt都会获得零值,这意味着它没有存储上一个值,每次都会重置。也许是因为我正在使用新的文件撰稿人?我不知道该怎么做。

如果我使用(fi,true),则分数是这样存储的-04060100,这意味着第一行不​​会走任何地方只有行才能只能阅读第一行,高分不能是多个正确的吗?

该怎么办?我是Java中此文件存储系统的新手。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2025-02-01 03:08:58

好吧,我会尽力让您理解。看,我只想将High Score仅存储到该文件中。因此,首先,当程序运行时,得分为0对吗?因此,corce.txt文件将在第一行中具有0。好的?在玩游戏后,我的得分为50,所以显然50大于0,然后得分。TXT文件将在第一行中有50个,而不是0。现在,我再次玩游戏并获得30分的得分,它不大于50,因此TXT文件将相同,它包含第一行中的高分,是50。当我得分100时,TXT将存储100。

是的...那是更清楚的对您试图做的事情的解释要比您在原始问题中写的内容。 (如果您在第一个瞬间就正确解释了它...)

解决方案是在您知道需要编写的文件之前,不要打开撰写文件的文件。逻辑应为:

  1. 打开文件以读取
  2. 包含高分
  3. 读取行,
  4. 如果当前分数大于高分,则
    1. 打开文件写
    2. 写新的高分
    3. 关闭。

您的代码当前要做的是在之前打开您甚至读取文件。这会截断文件,因此当您尝试读取当前高分时,信息已经被浪费了。

请注意,问题不是BufferedWriter bufferedReader 是问题所在。损坏是通过调用filewriter构造函数来造成的。 (Javadoc应该解释它。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文