Java Can';t替换文本文件中的字符串

Java Can';t替换文本文件中的字符串,java,Java,在此之后: 我有一个文本文件存储用户名、密码和该用户的最佳分数 试着做一个简单的问答游戏。我有一个注册面板,当用户注册时,我将数据存储在此文件中,并为新用户设置他的最佳分数0 每行的文本文件格式为 {username}{password}{bestScore} 当用户的得分超过其最佳得分时,我会尝试用最佳得分替换文本文件中的实际得分 好吧,回到那条线上来。我做了@meriton行发布的所有事情,但文本文件仍然没有更改。这是我的密码: if (gameData.getBestScore() <

在此之后:

我有一个文本文件存储用户名、密码和该用户的最佳分数

试着做一个简单的问答游戏。我有一个注册面板,当用户注册时,我将数据存储在此文件中,并为新用户设置他的最佳分数0

每行的文本文件格式为

{username}{password}{bestScore}

当用户的得分超过其最佳得分时,我会尝试用最佳得分替换文本文件中的实际得分

好吧,回到那条线上来。我做了@meriton行发布的所有事情,但文本文件仍然没有更改。这是我的密码:

if (gameData.getBestScore() < gameData.getScore()) {
            int oldBestScore = gameData.getBestScore();
            String oldLine = gameData.getCurrentUser() + " " + gameData.getCurrentUserPassword() + " " + oldBestScore;
            gameData.setBestScore(gameData.getScore());
            String newLine = gameData.getCurrentUser() + " " + gameData.getCurrentUserPassword() + " " + gameData.getBestScore();

            // TODO replace the points in the text file
            //first method
            /*try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Users\\Niki\\Desktop\\Java Projects\\QuizGame\\QuizGame\\usernames.txt"))))) {
                String line = br.readLine();

                while (line != null) {
                    if (line.contains(gameData.getCurrentUser())) {
                        String newLine = gameData.getCurrentUser() + " " + gameData.getCurrentUserPassword() + " " + gameData.getBestScore();
                        line = line.replace(line, newLine);
                        break;
                    }
                    line = br.readLine();
                }

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/

            //second method
            Path path = Paths.get("C:\\Users\\Niki\\Desktop\\Java Projects\\QuizGame\\QuizGame\\usernames.txt");
            Charset charset = StandardCharsets.UTF_8;


            String content = null;
            try {
                content = new String(Files.readAllBytes(path), charset);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(content);
            content = content.replaceAll(oldLine, newLine);
            System.out.println(content);

            gameOverPanel.gameOverLabel.setText("<html><h1>You didn't answer correctly!</h1><hr><h2>The correct answer is: " + gameData.getCurrentQuestion().getCorrectAnswer().getText() + "</h2><h3>Congratulations! New High Score: " + gameData.getBestScore() + "</h3></html>");
        }
        else {
            gameOverPanel.gameOverLabel.setText("<html><h1>You didn't answer correctly!</h1><hr><h2>The correct answer is: " + gameData.getCurrentQuestion().getCorrectAnswer().getText() + "</h2><h3>Your score: " + gameData.getBestScore() + "</h3></html>");
        }
    }
if(gameData.getBestScore()正确答案是:“+gameData.getCurrentQuestion().getCorrectAnswer().getText()+”,恭喜您!新的高分:“+gameData.getBestScore()+”);
}
否则{
gameOverPanel.gameOverLabel.setText(“您没有正确回答!
正确答案是:“+gameData.getCurrentQuestion().getCorrectAnswer().getText()+”您的分数:“+gameData.getBestScore()+”); } }
正如您所看到的,在编辑内容之前和编辑之后,I
println
都会进入控制台,一切正常。旧内容将替换为新内容,但文件不会用新内容更新

此外,我还尝试了我的方法,您可以在我的代码中看到//first-method注释下的注释部分。这种方法仍然不起作用。

这里:

System.out.println(content);
content = content.replaceAll(oldLine, newLine);
System.out.println(content);
它将更新内存中的字符串变量。这就是所有这些。但内存中的值和光盘上的文件之间并没有“神奇”的联系。该字符串变量既不知道也不关心您最初是否从文件中读取其内容

如果要更新文件内容;然后,您必须将更改后的字符串写回文件中。请参阅以了解如何做到这一点。

此处:

System.out.println(content);
content = content.replaceAll(oldLine, newLine);
System.out.println(content);
它将更新内存中的字符串变量。这就是所有这些。但内存中的值和光盘上的文件之间并没有“神奇”的联系。该字符串变量既不知道也不关心您最初是否从文件中读取其内容


如果要更新文件内容;然后,您必须将更改后的字符串写回文件中。请参阅以了解如何执行此操作。

尝试将变量的内容写入源文件

Files.write(path, content.getBytes(), StandardOpenOption.CREATE);
您刚刚将文件内容加载到内存中,并在
content
变量上应用了
replaceAll


但您必须将更改保存到源文件。

尝试将变量的内容写入源文件

Files.write(path, content.getBytes(), StandardOpenOption.CREATE);
您刚刚将文件内容加载到内存中,并在
content
变量上应用了
replaceAll


但您必须将更改保存到源文件中。

您没有写入文件,那么您为什么希望它更改?可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的谢谢!修正它并完全理解这个想法。欢迎您。请随意接受您喜欢的答案;-)是的。这就是为什么要求他调查此事;-)非常感谢。修正它并完全理解这个想法。欢迎您。请随意接受您喜欢的答案;-)是的。这就是为什么要求他调查此事;-)
Files.write(path, content.getBytes(), StandardOpenOption.CREATE);