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);

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...