为什么我调用这个方法生成的test.txt文件 里面写不进数据,test.txt文件能够生成里面可是空的没有数据private void logout(String message) {
PrintWriter out=null;
try{
out=new PrintWriter(new FileOutputStream                      ("c:\\test.txt",true));
out.write(new java.util.Date().toLocaleString()+"::Form ContextListener: " + message);
}
catch(Exception e){
out.close();
              e.printStackTrace();
    
}
}

解决方案 »

  1.   

    加上
    out.flush();

      

  2.   

    请问加上out.flush(); 起到什么作用 不是很明白
      

  3.   

    不是吧。 out=new PrintWriter(new FileOutputStream("c:\\test.txt",true));他这不是写参数true了麽。还用out.flush();麽?
      

  4.   

    PrintWriter(OutputStream out) 
              Create a new PrintWriter, without automatic line flushing, from an existing OutputStream. 
    PrintWriter(OutputStream out, boolean autoFlush) 
              Create a new PrintWriter from an existing OutputStream. 
      

  5.   

    加上out.close();就好了 谢谢各位
      

  6.   

    因为没有把缓冲区中的数据送到文件中,要flush。而close操作之前会做flush这一动作,所以你加了close以后就可以了。
      

  7.   

    我们用java来解决实际问题。java技术交流,讨论java的技术细节和最新技术。欢迎中高级程序员以及渴望学习java技术的初学者加入讨论。QQ群:3001581
      

  8.   

    恩,CLOSE不能放到 捕捉异常里面
      

  9.   

    原来自动刷新是在.close()时做的,学到了.
      

  10.   

    跟进去out.close() 方法里面看看就知道了