在Java中,使用BufferedWriter附加到文件有什么好处? [英] In Java, what is the advantage of using BufferedWriter to append to a file?

查看:103
本文介绍了在Java中,使用BufferedWriter附加到文件有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看以下示例



使用以下代码

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

比做什么有什么好处

  FileWriter fw = new FileWriter(outfilename); 

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

解决方案

Javadoc 就此主题提供了合理的讨论:


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

  PrintWriter out = new PrintWriter(new BufferedWriter(new 
FileWriter(foo。出)));

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


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



编辑



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




  • 较大的默认缓冲区大小。

  • 能够覆盖/自定义缓冲区大小。



为了进一步混淆水域, OutputStreamWriter 的java.html> Java 6实现实际上委托给 StreamEncoder ,它使用自己的缓冲区,默认大小为8192字节。并且 StreamEncoder 缓冲区是用户可配置的,尽管无法通过封闭的 OutputStreamWriter 直接访问它。 / p>

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

解决方案

The Javadoc provides a reasonable discussion on this subject:

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.

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 BufferedWriter will be much more efficient.

Edit

As per andrew's comment below, the FileWriter actually uses its own fixed-size 1024 byte buffer. This was confirmed by looking at the source code. The BufferedWriter sources, 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 BufferedWriter vs. FileWriter are limited to:

  • Larger default buffer size.
  • Ability to override/customize the buffer size.

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

这篇关于在Java中,使用BufferedWriter附加到文件有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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