在 Java 中,使用 BufferedWriter 附加到文件有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6976893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:06:45  来源:igfitidea点击:

In Java, what is the advantage of using BufferedWriter to append to a file?

java

提问by deltanovember

I'm looking at the following example

我正在查看以下示例

Which uses the following code

其中使用以下代码

try {
      BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
      out.write("aString");
      out.close();
    } 
catch (IOException e) {}

What's the advantage over doing

比做有什么优势

FileWriter fw = new FileWriter("outfilename");

I have tried both and they seem comparable in speed when it comes to the task of appending to a file one line at a time

我已经尝试了这两种方法,当涉及到一次向文件追加一行的任务时,它们的速度似乎相当

回答by aroth

The Javadocprovides a reasonable discussion on this subject:

Javadoc中提供了关于这个问题的一个合理的讨论:

In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,

 PrintWriter out    = new PrintWriter(new BufferedWriter(new 
     FileWriter("foo.out")));   

will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.

通常,Writer 立即将其输出发送到底层字符或字节流。除非需要提示输出,否则建议将 BufferedWriter 包裹在任何 write() 操作可能开销较大的 Writer 周围,例如 FileWriters 和 OutputStreamWriters。例如,

 PrintWriter out    = new PrintWriter(new BufferedWriter(new 
     FileWriter("foo.out")));   

将 PrintWriter 的输出缓冲到文件中。如果没有缓冲,每次调用 print() 方法都会导致字符转换为字节,然后立即写入文件,这可能非常低效。

If you're writing large blocks of text at once (like entire lines) then you probably won't notice a difference. If you have a lot of code that appends a single character at a time, however, a BufferedWriterwill be much more efficient.

如果您一次编写大块文本(如整行),那么您可能不会注意到差异。但是,如果您有很多代码一次附加一个字符,那么 aBufferedWriter会更有效率。

Edit

编辑

As per andrew's comment below, the FileWriteractually uses its own fixed-size 1024 byte buffer. This was confirmed by looking at the source code. The BufferedWritersources, on the other hand, show that it uses and 8192 byte buffer size (default), which can be configured by the user to any other desired size. So it seems like the benefits of BufferedWritervs. FileWriterare limited to:

根据下面安德鲁的评论,FileWriter实际上使用它自己的固定大小的 1024 字节缓冲区。通过查看源代码证实了这一点。的BufferedWriter来源,在另一方面,示出其使用和8192字节的缓冲区大小(默认值),其可以通过用户的任何其它期望的大小来配置。所以似乎BufferedWritervs.的好处FileWriter仅限于:

  • Larger default buffer size.
  • Ability to override/customize the buffer size.
  • 更大的默认缓冲区大小。
  • 能够覆盖/自定义缓冲区大小。

And to further muddy the waters, the Java 6 implementationof OutputStreamWriteractually delegates to a StreamEncoder, which uses its own buffer with a default size of 8192 bytes. And the StreamEncoderbuffer is user-configurable, although there is no way to access it directly through the enclosing OutputStreamWriter.

为了进一步搅浑水,在Java 6的实现OutputStreamWriter实际委托给StreamEncoder,使用它自己的缓冲区8192个字节的默认大小。并且StreamEncoder缓冲区是用户可配置的,尽管无法通过封闭的OutputStreamWriter.

回答by andrew cooke

this is explained in the javadocs for outputstreamwriter. a filewriter doeshave a buffer (in the underlying outputstreamwriter), but the character encoding converter is invoked on each call to write. using an outer buffer avoids calling the converter so often.

这在 outputstreamwriter 的 javadocs 中有解释。文件编写器确实有一个缓冲区(在底层的输出流编写器中),但是在每次调用 write 时都会调用字符编码转换器。使用外部缓冲区可以避免如此频繁地调用转换器。

http://download.oracle.com/javase/1.4.2/docs/api/java/io/OutputStreamWriter.html

http://download.oracle.com/javase/1.4.2/docs/api/java/io/OutputStreamWriter.html

回答by SJuan76

A buffer effectivity is more easily seen when the load is high. Loop the out.write a couple thousand of times and you should see a difference.

当负载高时,更容易看到缓冲效果。循环 out.write 几千次,您应该会看到不同之处。

For a few bytes passed in just one call probably the BufferedWriter is even worse (because it problably later calls FileOutputStream).

对于仅在一次调用中传递的几个字节,BufferedWriter 可能更糟(因为它可能稍后调用 FileOutputStream)。