何时刷新BufferedWriter [英] When to flush a BufferedWriter

查看:93
本文介绍了何时刷新BufferedWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java程序(Java 1.5)中,我有一个包装文件编写器的BufferedWriter,我多次调用write()...生成的文件非常大......

In a Java program (Java 1.5), I have a BufferedWriter that wraps a Filewriter, and I call write() many many times... The resulting file is pretty big...

在这个文件的行中,有些是不完整的...

Among the lines of this file, some of them are incomplete...

每次写东西时我是否需要调用flush(但是我怀疑它效率低下)或者使用BufferedWriter的另一种方法或者使用另一个类......?

Do I need to call flush each time I write something (but I suspect it would be inefficient) or use another method of BufferedWriter or use another class...?

(因为我写了很多行,我确实想要有一些非常有效的东西。)
什么是理想的潮红时刻? (当我达到BufferedWriter的容量时)...

(Since I've a zillion lines to write, I do want to have something quite efficient.) What would be the ideal "flushing" moment? (when I reach the capacity of the 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");


        }
      }
    }



<我刚刚发现不完整的行就是处理文件之前的行...所以当我从一个我分析的文件切换到另一个文件时就会出现...

I've just figured out that the lines which are incomplete are those just before "Processing file"... so it occurs when I'm switching from one file that I analyze to another...

结算:

dispatch(unit);

try {
  if (analysisOutput != null) {
    printFileInfo();
    analysisOutput.close();
  }
} 
catch (IOException ex) {
  ex.printStackTrace();
}

有时printFileInfo打印的信息不会出现在结果文件中。 。

Sometimes the information printed out by printFileInfo does not appear in the results file...

推荐答案

BufferedWriter 在填充缓冲区时已经刷新。来自 BufferedWriter的文档.write

The BufferedWriter will already flush when it fills its buffer. From the docs of BufferedWriter.write:


通常,此方法将给定数组中的字符存储到此流的缓冲区中,
根据需要将缓冲区刷新到基础流

(强调我的。)

BufferedWriter 基本上是为了将大量的小写写合并到更少的大写中,因为这通常更有效(但更难以编码)。你应该不需要做任何特殊的事情来让它正常工作,除了确保你在完成时用它冲洗它 - 并且调用 close() 将执行此操作并反正刷新/关闭基础编写器。

The point of BufferedWriter is basically to consolidate lots of little writes into far fewer big writes, as that's usually more efficient (but more of a pain to code for). You shouldn't need to do anything special to get it to work properly though, other than making sure you flush it when you're finished with it - and calling close() will do this and flush/close the underlying writer anyway.

换句话说,放松 - 只需写,写,写和关闭:)你通常只需要调用 flush 手动,如果你真的,真的需要数据现在在磁盘上。 (例如,如果你有一个永久记录器,你可能想要经常刷新它,以便无论谁在阅读日志之前都不需要等到缓冲区已满,然后才能看到新的日志条目!)

In other words, relax - just write, write, write and close :) The only time you normally need to call flush manually is if you really, really need the data to be on disk now. (For instance, if you have a perpetual logger, you might want to flush it every so often so that whoever's reading the logs doesn't need to wait until the buffer's full before they can see new log entries!)

这篇关于何时刷新BufferedWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆