【问题标题】:bufferwriter not writing to file [duplicate]bufferedwriter不写入文件[重复]
【发布时间】:2011-07-18 19:35:44
【问题描述】:

bufferwriter 没有写入文件。谁能告诉我可能是什么问题

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;


public class main {
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        Writer output = null;
        File file = new File("D:/junk/CI_CSSOIDs sql_query/orphans.log");
        output = new BufferedWriter(new FileWriter(file));

        java.io.FileReader fr = new FileReader( "D:/junk/CI_CSSOIDs ql_query/SQL_CSSO.log" ) ;
        java.io.BufferedReader reader = new BufferedReader( fr ) ;
        int orphancount=0;
        String line = null ;
        int count =1;
        while( ( line = reader.readLine() ) != null )
        {         
            String words[]=line.split(" ");
            for (int i =0;i<words.length;i++){
                if (words[i].length()==32){
                    String CIline=null;
                    java.io.FileReader CIfr = new FileReader( "D:/junk/CI_CSSOIDs sql_query/CI.log" ) ;
                    java.io.BufferedReader CIreader = new BufferedReader( CIfr ) ;
                    boolean orphan = true;
                    while((CIline=CIreader.readLine())!=null){
                        if (CIline.contains(words[i])){
                            orphan=false;
                            break;
                            }                       
                    }
                    if(orphan){
                        orphancount++;
                        output.write("####"+words[i]+"*****\n");
                        System.out.println(words[i]+" : is an orphan CSSOID");
                    }                   
                }
            }       
            count++;        
        }
        System.out.println("Orphan count is :"+orphancount);
    }
}

【问题讨论】:

    标签: java file-io bufferedwriter


    【解决方案1】:

    尝试flush() 然后close() 你的BufferedWriter 写入数据后。

    【讨论】:

      【解决方案2】:

      A BufferedWriter 缓冲写入。这意味着它将数据保留在内存中,直到缓冲区耗尽或您刷新/关闭写入器。这样做是为了提高性能。

      我建议您关闭所有流/读取器/写入器,这也可以解决您的问题。

      【讨论】:

      • 谢谢彼得。你的回答很有帮助。我添加了 output.flush();在写语句之后。
      • 过于频繁地调用flush会影响性能(违背了使用BufferedWriter的目的)我建议你不要flush并关闭所有应该关闭的资源。
      【解决方案3】:

      写完后关闭BufferedWriter。

      output.close();
      

      【讨论】:

        【解决方案4】:

        你既不刷新()也不关闭()作者。由于它是一个 缓冲 写入器,所有内容都存储在缓冲区中,直到您执行其中之一。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-18
          • 1970-01-01
          • 1970-01-01
          • 2012-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多