【问题标题】:PrintWriter writes blank filePrintWriter 写入空白文件
【发布时间】:2016-06-12 11:49:34
【问题描述】:

我想更改用户的分数,但不是用新分数覆盖文件,而是创建一个空文件。 这些行是这样写的:“username-password-wins-losses-ties”

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) {} 

感谢任何帮助。

【问题讨论】:

    标签: java file printwriter


    【解决方案1】:

    您正在跳过第一行和每个奇数行,更改如下代码

        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();
    

    【讨论】:

    • 我用你的代码替换了我的代码,但是我写入后文件仍然是空的。
    • 更新答案,输入文件中有文本吗?
    • 首先我注册一个用户,并将其保存到“users.txt”文件中。该行如下所示:“username-password-wins-losses-ties”(user-pass-0-0-0)。然后我玩游戏,这段代码应该将所有更改和未更改的行放入“zlepek”字符串。最后,它应该将“zlepek”字符串中的任何内容粘贴回文件中,覆盖现有文本。相反,它只是用空白文件替换所有内容。
    • 如果你想写在同一个文件上,你必须在写之前关闭阅读器,查看更新的答案
    • 尝试运行此代码时似乎返回错误:线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException 该文件似乎没有更改。
    【解决方案2】:

    问题出在while(br.readLine()!=null)这一行,你读了这行但没有保存,所以它不知道它在哪里,你读了这行后,该行总是空的,不保存。

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

    【讨论】:

      【解决方案3】:
      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) {}
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多