微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/java/129096.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐