何时刷新缓冲写入器
在Java程序(Java 1.5)中,我有一个BufferedWriter,它包装了一个Filewriter,我多次调用write()...生成的文件非常大...
在此文件的行中,其中一些是不完整的...
我是否需要在每次编写内容时调用 flush(但我怀疑这会是低效的),或者使用 BufferedWriter 的另一种方法或使用另一个类...?
(由于我有无数行要写,我确实希望有一些非常有效的东西。什么是理想的“冲洗”时刻?(当我达到缓冲器的容量时)...
初始化:
try {
  analysisOutput = new BufferedWriter(new FileWriter(
      "analysisResults", true));
  analysisOutput.newLine();
  analysisOutput.write("Processing File " + fileName + "\n");
} 
catch (FileNotFoundException ex) {
  ex.printStackTrace();
} 
catch (IOException ex) {
  ex.printStackTrace();
}
写作:
private void printAfterInfo(String toBeMoved,HashMap<String, Boolean> afterMap, Location location)
  throws IOException {
    if(afterMap != null) {
      for (Map.Entry<String, Boolean> map : afterMap.entrySet()) {
        if (toBeMoved == "Condition") {
          if (1 <= DEBUG)
            System.out.println("###" + toBeMoved + " " + location + " "
                + conditionalDefs.get(conditionalDefs.size() - 1)
                + " After " + map.getKey() + " "
                + map.getValue() + "\n");
          analysisOutput.write("###" + toBeMoved + " " + location + " "
              + conditionalDefs.get(conditionalDefs.size() - 1)
              + " After " + map.getKey() + " " + map.getValue()
              + "\n");
        } else {
          if (1 <= DEBUG)
            System.out.println("###" + toBeMoved + " " + location + " "
                + map.getKey() + " After " 
                + map.getValue() + "\n");
          if (conditionalDefs.size() > 0)
            analysisOutput.write("###" + toBeMoved + " " + location + " "
                + conditionalDefs.get(conditionalDefs.size() - 1) + " "
                + map.getKey() + " After " + map.getValue()
                + "\n");
          else
            analysisOutput.write("###" + toBeMoved + " " + location + " " + map.getKey() + " After " + map.getValue() + "\n");
        }
      }
    }
我刚刚发现不完整的行是“处理文件”之前的行......所以当我从我分析的一个文件切换到另一个文件时,就会发生这种情况......
关闭:
dispatch(unit);
try {
  if (analysisOutput != null) {
    printFileInfo();
    analysisOutput.close();
  }
} 
catch (IOException ex) {
  ex.printStackTrace();
}
有时,printFileInfo 打印出来的信息不会出现在结果文件中...
 
					 
				 
				    		 
				    		 
				    		