替换 java 中文本文件的内容

Replacing the contents of a text file in java

我有一个名为 leaderboard.txt 的文本文件,我将其作为资源流加载并将内容读取到字符串(这是一个短文本文件),然后我在字符串中替换了我想要的内容在同一位置创建新的 leaderboard.txt 文件或覆盖以前的 leaderboard.txt 文本文件。

这是我覆盖文件的代码,但它抛出了 java.lang.NullPointerException

package tools;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import screens.PlayMenu;


public class Leaderboard
{
    private static BufferedReader leaderboardIn;

    public static void checkLeaderBoard(int time, int currentLevel)
    {
        try
        {
            InputStream filePath = Leaderboard.class
                    .getResourceAsStream("/Other/Leaderboard.txt");
            leaderboardIn = new BufferedReader(new InputStreamReader(filePath));

            String input = "";
            for (int level = 1; level < currentLevel; level++)
                input = leaderboardIn.readLine() + '\n';
            String line = leaderboardIn.readLine();
            int leaderboardTime = Integer.parseInt(line.substring(0,
                    line.indexOf(' ')));
            if (leaderboardTime > time)
            {
                line = time + " " + PlayMenu.playerName.toUpperCase() + '\n';
                input += line;
                while ((line = leaderboardIn.readLine()) != null)
                    input += line + '\n';
                 PrintWriter fileOut = 
                           new PrintWriter(
                                 new File(Leaderboard.class.getResource("/Trial and Error/resources/Other/Leaderboard.txt").getPath()));
                fileOut.write(input);
                fileOut.close();
            }
            filePath.close();
            leaderboardIn.close();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

这是我的实际文本文件所在位置的图片:

如有任何帮助,我们将不胜感激。

编辑 这是堆栈跟踪(也在图像中)

Exception in thread "Thread-0" java.lang.NullPointerException
at tools.Leaderboard.checkLeaderBoard(Leaderboard.java:39)
at entities.Player.onExit(Player.java:91)
at entities.Player.update(Player.java:38)
at screens.GameState.update(GameState.java:27)
at main.Game.update(Game.java:62)
at main.Game.run(Game.java:111)
at java.lang.Thread.run(Unknown Source)

我本来可以将其作为评论发布的,但显然我没有足够的声誉来这样做,所以...

为什么不直接使用 Files.readAllLines() and Files.write()

正如我所见,堆栈跟踪指向 Line 39 请编写以下代码并查看其输出。

Leaderboard c = new Leaderboard();
Class cls = c.getClass();

// finds resource relative to the class location
URL url = cls.getResource("/Other/Leaderboard.txt");
System.out.println("Value = " + url);

// finds resource relative to the class location
url = cls.getResource("/Trial and Error/resources/Other/Leaderboard.txt");
System.out.println("Value = " + url);

如果最后一个 returns null,则找不到具有该名称的资源。

此外,尝试以下

Leaderboard c = new Leaderboard();
    Class cls = c.getClass();
fileOut = new PrintWriter(new File(cls.getResource("/Other/Leaderboard.txt").getPath()));

而不是

 PrintWriter fileOut = new PrintWriter(new File(Leaderboard.class.getResource("/Trial and Error/resources/Other/Leaderboard.txt").getPath()));