java – FileWriter vs BufferedWriter

前端之家收集整理的这篇文章主要介绍了java – FileWriter vs BufferedWriter前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道FileWriter是否被缓冲.

this SO问题,它似乎是,但是在this SO问题似乎不是.(这将是每次写(…)被调用的系统调用.

所以基本上阅读那两个Q& A我有点困惑.有人能够清楚地解释出来吗?

提前致谢.

编辑:通过阅读this API解决了问题我引用了相关部分:

Each invocation of a write() method causes the encoding converter to
be invoked on the given character(s). The resulting bytes are
accumulated in a buffer before being written to the underlying output
stream. The size of this buffer may be specified,but by default it is
large enough for most purposes. Note that the characters passed to the
write() methods are not buffered.

For top efficiency,consider wrapping an OutputStreamWriter within a
BufferedWriter so as to avoid frequent converter invocations. For
example:

Writer out = new BufferedWriter(new
OutputStreamWriter(System.out));

由于FileWriter扩展了OutputStreamWriter,它也适用于它.

谢谢你的时间,我知道我问了一些非常具体的事情.

解决方法

FileWriter没有缓冲,你必须使用BufferedWriter作为包装器:
final int myBufferSize = 2048;

Writer myWriter = new BufferedWriter(new FileWriter,myBufferSize);

猜你在找的Java相关文章