为什么我的BufferedReader没有正确读取我的文件?

时间:2014-05-19 07:48:36

标签: java bufferedreader bufferedwriter

我有一个小程序,它从.txt文件读取一个数字,递增它,并在每次运行时将其打印回文件:

public class ClientCounter {

    int count = 0;
    String input = "";

    public int addClient() {

        try (
            PrintWriter out = new PrintWriter(new FileWriter("test.txt"));
            BufferedReader in = new BufferedReader(new FileReader("test.txt"));
        ) {
            input = in.readLine();

            System.out.println("Input line is " + input);

            if (input != null)
                count = Integer.parseInt(input);

            System.out.println("Client count is: " + count);
            count++;

            System.out.println("After increment, count is: " + count);

            String output = Integer.toString(count);
            System.out.println("Output is " + output);

            out.println(output);

        } catch(FileNotFoundException e) {
            System.out.println("FNFE!");
            System.exit(1);
        } catch(IOException e) {
            System.out.println("IOE!");
            System.exit(1);
        }

        return count;

    }

但是,每次我尝试运行它,我都会得到这个输出:

Input line is null
Client count is: 0
After increment, count is: 1
Output is 1

我的test.txt文件始终只包含一个数字1

知道为什么会这样吗?

5 个答案:

答案 0 :(得分:1)

每次重写新文件时。

尝试如下,第二个参数(true)表示使用现有文件。

 PrintWriter out = new PrintWriter(new FileWriter("test.txt",true));

以上回答是错误的......以下更新答案......

已编辑的代码:可能 user270349 是正确的。下面的代码对我有用。

public class ClientCounter {

static int count = 0;
static String input = "";

public static void main(String args[]){
    addClient();
}

public static int addClient() {

    try {
        BufferedReader in = new BufferedReader(new FileReader("test.txt"));
        input = in.readLine();
        if (input != null)
            count = Integer.parseInt(input);
        System.out.println("Client count is: " + count);
        count++;
        in.close();

        System.out.println("After increment, count is: " + count);

        PrintWriter out = new PrintWriter(new FileWriter("test.txt"));
        String output = Integer.toString(count);
        System.out.println("Output is " + output);

        out.println(output);
        out.flush();out.close();

    } catch(FileNotFoundException e) {
        System.out.println("FNFE!");
        System.exit(1);
    } catch(IOException e) {
        System.out.println("IOE!");
        System.exit(1);
    }

    return count;
}
}

答案 1 :(得分:1)

完成后,您必须立即关闭文件。将同一个文件打开两次也是个坏主意。 也许你应该这样做:

BufferedReader in = ...
try {
  count = Integer.parseInt(in.readLine());
} finally {
  in.close();
}
count++;
  PrintWritter out = ...
try {
  out.println(count);
} finally {
  out.close();
}

答案 2 :(得分:0)

您没有关闭该文件。当程序结束且缓冲区尚未写入磁盘时,您的修改将丢失。

答案 3 :(得分:0)

从文件中读取内容...

File f = new File("test.txt"); 
     

FileReader fr = new FileReader(f);

     

BufferedReader br = new BufferedReader(fr);

     

String str = null;

     

while((str = br.readLine())!= null){

     

// Line存储在'String s'中。用str

做任何你想做的事      

}

     

br.close(); //不要忘记通过调用close方法关闭BufferedReader的实例。

用于在文件中写一些东西......

File f = new File("newfile.txt");
String content = "This is something I want to write";
BufferedWriter bw = new BufferedWriter(f);
bw.write(content);
bw.close();

答案 4 :(得分:0)

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class BufferedReaderTest {


    public static void main(String args[]) {
         int count = 0;
         String input;

        try {
            BufferedReader br = new BufferedReader(new FileReader("E:\\this.txt"));
            PrintWriter pw = new PrintWriter(new FileWriter("E:\\this.txt"));
            input = br.readLine();
            if (input != null)
                count = Integer.parseInt(input);
            System.out.println("Client count is: " + count);

            System.out.println("After increment, count is: " + (++count));

            String output = Integer.toString(count);
            System.out.println("Output is " + output);

            pw.println(output);
            pw.flush();
            pw.close();

        } catch (FileNotFoundException e) {
            System.out.println("FNFE!");
            System.exit(1);
        } catch (IOException e) {
            System.out.println("IOE!");
            System.exit(1);
        }
    }
}