2017-04-03 54 views
0

替換字符串在此之後thread不能在一個文本文件

我有一個文本文件中存儲的用戶名密碼,bestscore該用戶。

試圖做一個簡單的問答遊戲。我有一個註冊面板,當用戶註冊時,我將數據存儲在此文件中,併爲新用戶創建最佳分數0。

爲每一個行中的文本文件格式是

{用戶名} {密碼} {bestScore}

而且當用戶比他bestScore更多我嘗試更換實際得分在具有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>"); 
     } 
    } 

正如你可以看到我println編輯內容之前和之後的控制檯和一切正常存在。舊內容將替換爲新內容,但文件不會更新爲新內容。

此外,我試圖按照我的方式做到這一點,你可以在我的代碼// //第一個方法評論下看到註釋部分。這種方式仍然沒有奏效。

+0

那麼,你沒有寫入文件,那麼你爲什麼會期望它改變? – Mena

+0

http://stackoverflow.com/questions/23466179/java-replace-specific-string-in-textfile – freedev

+0

可能重複[java替換文本文件中的特定字符串](http://stackoverflow.com/questions/ 23466179/java-replace-specific-string-in-textfile) – freedev

回答

1

這裏:

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

在內存中更新您的字符串變量。這就是這一切。但是,內存中的值與光盤上的文件之間沒有「魔術」連接。那個字符串變量既不知道也不關心你最初從文件中讀取它的內容。

如果要更新文件的內容;那麼您必須將更改的字符串寫回您的文件。有關如何做到這一點的想法,請參見here

+0

謝謝!修復它並完全理解這個想法。 –

+0

:)是正確答案? – freedev

+1

不客氣。隨意接受您的首選答案,然後;-) – GhostCat

1

嘗試將變量的內容寫入源文件。

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

你剛纔加載文件在內存中的內容,並在content變量應用replaceAll

但您必須將更改保存到源文件中。

+0

謝謝! Evertything現在正在工作。 –