PrintWriter 写入空白文件

PrintWriter writes blank file

我想更改用户的分数,但不是用新分数覆盖文件,而是创建一个空文件。 行是这样写的:"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) {} 

感谢任何帮助。

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

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

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

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

另外你是在同一个文件上读写,你不能同时在同一个文件上读写。如果您愿意,可以将其写入临时文件并稍后重命名。

如果要写入同一个文件,写入前必须关闭reader,见下文

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