PrintWriter写入空白文件

时间:2016-06-12 11:49:34

标签: java file printwriter

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

感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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();

答案 2 :(得分: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) {}
}