在java中替换文本文件的内容

时间:2015-06-12 03:31:01

标签: java output

我有一个名为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();
        }
    }
}

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

任何形式的帮助都将受到赞赏。

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

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)

2 个答案:

答案 0 :(得分:3)

我会将此作为评论发布,但显然我没有足够的声誉来做到这一点,所以......

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

答案 1 :(得分:1)

我可以看到堆栈跟踪,它指向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);

如果最后一个返回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()));