package com.han;import java.io.*;public class File8 {
public static void main(String args[]) throws IOException{
InputStreamReader iin=new InputStreamReader(System.in );
BufferedReader br=new BufferedReader(iin);
FileWriter fw1=new FileWriter("C:\\Users\\Administrator\\Desktop\\file4.txt");
BufferedWriter bw=new BufferedWriter(fw1);
String s;
while(true){
System.out.println("输入一个字符串");
System.out.flush();//清空输出缓冲区
s=br.readLine();
if(s.length()==0) break;
bw.write(s);
bw.newLine();
}
bw.close();

}
}
大神帮看看,怎么不报错但是写不进文本文档的?

解决方案 »

  1.   

    import java.io.*;public class File8 {
    public static void main(String args[]) throws IOException{
    InputStreamReader iin=new InputStreamReader(System.in );
    BufferedReader br=new BufferedReader(iin);
    FileWriter fw1=new FileWriter("C:\\file4.txt");
    BufferedWriter bw=new BufferedWriter(fw1);
    String s;
    while(true){
    System.out.println("输入一个字符串");
    System.out.flush();//清空输出缓冲区
    s=br.readLine();
    if(s.length()==0) break;
    bw.write(s,0,s.length());
    bw.newLine();
    }
    bw.close();}
    }
      

  2.   

    这个程序不是我输入字符串后,字符串会在c盘file4.txt文本中保存?为什么保存不了啊?
      

  3.   

    bw.newLine(); 后面加上bw.flush();一定可以
      

  4.   

    加bw.flush();或者用try{}catch(){}finally{}在finally里面关闭各文件流,在main后抛异常是非常不严肃的。
      

  5.   

    flush
    public void flush() throws IOException刷新该流的缓冲。 
    指定者:
    接口 Flushable 中的 flush
    指定者:
    类 Writer 中的 flush
    抛出: 
    IOException - 如果发生 I/O 错误
    不写就无法把缓存中数据写入流
      

  6.   

    哥们,,bw.newLine(); 后面加上bw.flush();