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

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

问题描述

在 Java 程序 (Java 1.5) 中,我有一个包装 Filewriter 的 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 + "
");
} 
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() + "
");

          analysisOutput.write("###" + toBeMoved + " " + location + " "
              + conditionalDefs.get(conditionalDefs.size() - 1)
              + " After " + map.getKey() + " " + map.getValue()
              + "
");
        } else {
          if (1 <= DEBUG)
            System.out.println("###" + toBeMoved + " " + location + " "
                + map.getKey() + " After " 
                + map.getValue() + "
");
          if (conditionalDefs.size() > 0)
            analysisOutput.write("###" + toBeMoved + " " + location + " "
                + conditionalDefs.get(conditionalDefs.size() - 1) + " "
                + map.getKey() + " After " + map.getValue()
                + "
");
          else
            analysisOutput.write("###" + toBeMoved + " " + location + " " + map.getKey() + " After " + map.getValue() + "
");


        }
      }
    }

我刚刚发现不完整的行是在处理文件"之前的那些行......所以当我从一个我分析的文件切换到另一个文件时会发生这种情况......

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:

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

Ordinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed.

(强调我的.)

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天全站免登陆