求教各位大侠:我是java的初学者,现在在使用java进行文件操作时有问题。
问题:我在java程序中select数据库中的表(结果大约几百行),结果在屏幕上输出无误,但写入文件却是空的,求教各位大侠,java中文件写入的具体操作,望能指点一二,谢谢! 
附带:部分代码
while(rs.next())
    {
     id=rs.getInt("ID");
     type=rs.getInt("MSGTYPE");
     //System.out.println("ID="+id+" "+"MSGTYPE="+type);
     result="ID="+id+" "+"MSGTYPE="+type;
     try
     {
     tofile=new FileWriter("result.txt");
     out=new BufferedWriter(tofile);
     //System.out.println(result);
       out.write(result,0,result.length());
       out.newLine();
      }
     catch(IOException e)
     {
     System.out.println("write to file fail!");
     System.out.println("The error is:"+e);
     }
    }

解决方案 »

  1.   

    应为用了BufferedWriter,所以写操作不会马上执行,会先放入缓存,这样可以提升性能
    楼主的问题是写的数据量太小,结束的时候没有对BufferedWriter进行flush,导致缓存的内容没有写入文件可以添加out.flush(),或者out.close()
      

  2.   

    万分谢谢believefym(暮色,miss,迷失,miss) 的解答,我也曾经加过out.flush(),out.close()加了之后程序运行就十分缓慢,而且最后result.txt中只有一行结果(不是我在屏幕上看到的那么多),不知何故?再次谢谢您!!!
      

  3.   

    补:前面代码的定义部分
                 int id;
        int type;
        String result;
        FileWriter tofile;
        BufferedWriter out;
      

  4.   

    out=new BufferedWriter(tofile);
    这个在while循环内部,就是说你有几条记录,就要重新打开这个io,记录越多性能就越差了,而且一旦打开文件就会被覆盖,所以原来的内容会消失你把out=new BufferedWriter(tofile);放到while前面
    在while结束之后对out进行close就可以了
      

  5.   

    最终代码:
    int id;
        int type;
        String result;
        FileWriter tofile;
        BufferedWriter out=null;
        try
         {
         tofile=new FileWriter("result.txt");
            out=new BufferedWriter(tofile);
         //System.out.println(result);
           
          }
        catch(IOException e)
         {
         System.out.println("write to file fail!");
         System.out.println("The error is:"+e);
         }
        
        while(rs.next())
        {
         id=rs.getInt("ID");
         type=rs.getInt("MSGTYPE");
         //System.out.println("ID="+id+" "+"MSGTYPE="+type);
         result="ID="+id+" "+"MSGTYPE="+type;
         try
         {
         out.write(result,0,result.length());
           out.newLine();
         }
         catch(IOException e)
         {
         System.out.println("out.write to file fail!");
         System.out.println("The error is:"+e);
         }
        
        }
      

  6.   

    没看到flush或者close,没问题的吗?
      

  7.   

    不好意思,没有贴全
        int id;
        int type;
        String result;
        FileWriter tofile;
        BufferedWriter out=null;
        try
         {
         tofile=new FileWriter("result.txt");
            out=new BufferedWriter(tofile);
         //System.out.println(result);
           
          }
        catch(IOException e)
         {
         System.out.println("write to file fail!");
         System.out.println("The error is:"+e);
         }
        
        while(rs.next())
        {
         id=rs.getInt("ID");
         type=rs.getInt("MSGTYPE");
         //System.out.println("ID="+id+" "+"MSGTYPE="+type);
         result="ID="+id+" "+"MSGTYPE="+type;
         try
         {
         out.write(result,0,result.length());
           out.newLine();
         }
         catch(IOException e)
         {
         System.out.println("out.write to file fail!");
         System.out.println("The error is:"+e);
         }
        
        }
        try
        {
         out.close();
        }
        catch(IOException e)
        {
         System.out.println("close file fail!");
         System.out.println("The error is:"+e);
        }
      }
      

  8.   

    每次while都写文件的确不是好想法~~