2016-06-12 84 views
-1

我想更改用户的分数,但不是用新分数覆盖文件,而只是创建一个空文件。 该行这样写的:“用户名 - 密码 - 胜 - 负 - 关系”PrintWriter写入空白文件

try { 
     BufferedReader br = new BufferedReader(new FileReader("users.txt")); 
     PrintWriter pw = new PrintWriter(new FileWriter("users.txt")); 
     String[] tab = null; 
     String zlepek = ""; 
     while(br.readLine()!=null) { 
      String line = br.readLine(); 
      tab = line.split("-"); 
      int countLoss = Integer.parseInt(tab[3]+plusLoss); 
      int countWin = Integer.parseInt(tab[2]+plusWin); 
      int countTie = Integer.parseInt(tab[4]+plusTie); 
      if(tab[0].equals(loginUser.user)) { 
       String newScore = (tab[0] + "-" + tab[1] + "-" + countWin + "-" + countLoss + "-" + countTie + "\n"); 
       zlepek = zlepek+newScore; 
      } else { 
       String oldScore = (tab[0] + "-" + tab[1] + "-" + tab[2] + "-" + tab[3] + "-" + tab[4] + "\n"); 
       zlepek = zlepek+oldScore; 
      } 
     } 
     pw.print(zlepek); 
     pw.close(); 
     br.close(); 

     plusWin = 0; 
     plusLoss = 0; 
     plusTie = 0; 

    } catch(IOException e) {} 

任何帮助表示赞赏。

回答

0

你跳过第一行和每一个奇数行,改变像下面

String line = null; 
    while ((line = br.readLine()) != null){ 
      // code 
    } 

还你正在阅读,并在同一文件中写入代码,就无法读取和写入同时在一个文件上。如果你想要的话,你可以把它写到一个临时文件并稍后重命名。

如果你想在同一个文件写的,你必须写之前关闭的读者,请参见下面

 BufferedReader br = new BufferedReader(new FileReader("users.txt")); 
     String line = null; 
     StringBuilder sb = new StringBuilder(); 
     String zlepek = ""; 

     while ((line = br.readLine()) != null) { 
      zlepek = // logic to get value 
      sb.append(zlepek).append("\n"); 
     } 
     br.close(); 

     PrintWriter pw = new PrintWriter(new FileWriter("users.txt")); // pass true to append 
     pw.print(sb); 
     pw.close(); 
+0

我代替我的代码与你,但文件仍然是空后,我写的它。 – n3onis

+0

更新回答,你有输入文件中的任何文字吗? – Saravana

+0

首先我注册一个用户,并将其保存到“users.txt”文件中。该行如下所示:“username-password-wins-losses-ties”(user-pass-0-0-0)。然后我玩游戏,这段代码应该将所有已更改和未更改的行都放入“zlepek”字符串中。最后,它应该将“zlepek”字符串中的所有内容粘贴回文件中,覆盖现有文本。相反,它只是用空白文件替换所有内容。 – n3onis

0

问题是与这条线while(br.readLine()!=null),你读了行但没有保存它,所以它不知道它在哪里,并且在你读取行并不保存它之后,行将一直是空的。

尝试删除此String line = br.readLine();并在一段时间内阅读它。定义String line;while((line = br.readLine()) != null){ // do something}

0
try { 
     String[] tab = null; 
     String line = null; 
     tab = line.split("-"); 
     int countLoss = Integer.parseInt(tab[3]+plusLoss); 
     int countWin = Integer.parseInt(tab[2]+plusWin); 
     int countTie = Integer.parseInt(tab[4]+plusTie); 

     BufferedReader br = new BufferedReader(new FileReader("users.txt")); 
     StringBuilder sb = new StringBuilder(); 
     String zlepek = ""; 

     while ((line = br.readLine()) != null) { 
      if(tab[0].equals(loginUser.user)) { 
       String newScore = (tab[0] + "-" + tab[1] + "-" + countWin + "-" + countLoss + "-" + countTie + "\n"); 
       zlepek = zlepek+newScore; 
       } else { 
        String oldScore = (tab[0] + "-" + tab[1] + "-" + tab[2] + "-" + tab[3] + "-" + tab[4] + "\n"); 
        zlepek = zlepek+oldScore; 
       } 
      sb.append(zlepek).append("\n"); 
     } 
     br.close(); 

     PrintWriter pw = new PrintWriter(new FileWriter("users.txt")); // pass true to append 
     pw.print(sb); 
     pw.close(); 

     plusWin = 0; 
     plusLoss = 0; 
     plusTie = 0; 

    } catch(IOException e) {} 
}