BufferedReader bfr = null;
BufferedWriter bfw = null;
try {
bfr = new BufferedReader(new InputStreamReader(System.in));
bfw = new BufferedWriter(new FileWriter("c:/abc.txt")); 
int len = 0;
String text = bfr.readLine();
while(!(text.equals("exit"))){
 
bfw.write(text);
bfw.newLine();
//text = bfr.readLine();
}
bfw.flush();
上面的循环部分,如果把上面的一段代码注释掉了的话,则文件没发终止了,好像一直在循环,
而且生成的文件里面全是重复的,而且生成的txt文件很大。
局部定义的bfr.readLine()和上面定义的不一样嘛?

解决方案 »

  1.   

    String text = bfr.readLine();
                while(!(text.equals("exit"))){
                     
                    bfw.write(text);
                    bfw.newLine();
                    //text = bfr.readLine();
                }改成 String text = "";
    while (!(text = bfr.readLine()).equals("exit")) {
    bfw.write(text);
    bfw.newLine();
    //text = bfr.readLine();
    }
    bfw.flush();
      

  2.   


    我改成
    while(!(text=bfr.readLine()).equals("exit")){
     
    bfw.write(text);
    bfw.newLine();
    }
    bfw.flush();这样就不行了,数据没有写入到文件呢, while(!(text.equals("exit"))){
     
    bfw.write(text);
    bfw.newLine();
    text = bfr.readLine();//继续写后面的数据
    }
    bfw.flush();这样就OK了啊~
      

  3.   

    1楼可以,但是生成的内容全部都是重复的啊,。而且生成的txt文件100多MB
      

  4.   

    怎么可能 public static void main(String args[]) throws Exception { BufferedReader bfr = null;
    BufferedWriter bfw = null;
    bfr = new BufferedReader(new InputStreamReader(System.in));
    bfw = new BufferedWriter(new FileWriter("c:/abc.txt"));
    String text = "";
    while (!(text = bfr.readLine()).equals("exit")) {
    bfw.write(text);
    bfw.newLine();
    }
    bfw.flush();
    }