0

Following this thread:

I have a text file storing usernames password and bestscore for that user.

Trying to make a simple quiz game. I have a signup panel and when user signs up I store the data in this file and make his best score 0 for the new user.

The text file format for every single line is

{username} {password} {bestScore}

And when the user makes more than his bestScore i try to replace the actual score in the text file with the bestScore.

Well, back to that thread. I did everything line @meriton posted but the text file still hadn't changed. Here is my code:

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>");
        }
    }

As you can see I println before editing the content and after that to the console and everything is ok there. The old content is replaced with the new one but the file is not updated with the new content.

Also i tried to do it my way you can see the commented section in my code under the //first method comment. That way still didn't work.

Community
  • 1
  • 1
j.DOE
  • 35
  • 6
  • Well, you're not writing to the file, so why would you expect for it to change? – Mena Apr 03 '17 at 12:11
  • possible duplicate of http://stackoverflow.com/questions/23466179/java-replace-specific-string-in-textfile – freedev Apr 03 '17 at 12:15
  • Possible duplicate of [java replace specific string in textfile](http://stackoverflow.com/questions/23466179/java-replace-specific-string-in-textfile) – freedev Apr 03 '17 at 12:15

2 Answers2

1

Here:

System.out.println(content);
content = content.replaceAll(oldLine, newLine);
System.out.println(content);

That updates your String variable in memory. That's all this does. But there is no "magic" connection between that value in your memory and the file on disc. That string variable does neither know nor care that you read its content from a file initially.

If you want to update the file content; then you have to write back the changed string into your file. See here for ideas how to do that.

Community
  • 1
  • 1
GhostCat
  • 127,190
  • 21
  • 146
  • 218
1

Try to write the content of the variable into the source file.

Files.write(path, content.getBytes(), StandardOpenOption.CREATE);

You have just loaded the content of file in memory and applied the replaceAll on the content variable.

But you must save change into source file.

freedev
  • 17,230
  • 4
  • 83
  • 98