简体   繁体   English

替换Java中文本文件的内容

[英]Replacing the contents of a text file in java

I have a text file named leaderboard.txt, I load it up as a resource stream and read the contents to a string (it's a short text file) then I replace something in the string after that I want to create a new leaderboard.txt file in the same location or overwrite the previous leaderboard.txt text file. 我有一个名为leaderboard.txt的文本文件,我将其加载为资源流,并将内容读取到字符串(这是一个短文本文件),然后在该字符串中替换某些内容,然后创建一个新的leaderboard.txt。文件放在同一位置或覆盖以前的Leaderboard.txt文本文件。

Here is my code for overwriting the file, but it throws a java.lang.NullPointerException 这是我覆盖文件的代码,但是会抛出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();
        }
    }
}

Here's a picture of where my actual text file is: 这是我的实际文本文件的位置的图片: 在此处输入图片说明

Any kind of help would be appreciated. 任何帮助将不胜感激。

Edit Here's the stack trace (which is also in the image) 编辑这是堆栈跟踪(也在图像中)

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)

I would have posted this as a comment, but apparently I don't have enough reputation to do that, so... 我本可以将此作为评论发布,但是显然我没有足够的声誉来做到这一点,所以...

Why not just use Files.readAllLines() and Files.write() ? 为什么不只使用Files.readAllLines()Files.write()

As I can see stack trace, which is pointing at Line 39 Please write following code and see their output. 如我所见,它指向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);

If last one returns null , then resource with the name is not found. 如果最后一个返回null ,则找不到名称相同的资源。

Additionally, Try following 此外,请尝试以下

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

instead of 代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM