加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – FileWriter vs BufferedWriter

发布时间:2020-12-15 03:11:56 所属栏目:Java 来源:网络整理
导读:我想知道FileWriter是否被缓冲. 在this SO问题,它似乎是,但是在this SO问题似乎不是.(这将是每次写(…)被调用的系统调用. 所以基本上阅读那两个Q A我有点困惑.有人能够清楚地解释出来吗? 提前致谢. 编辑:通过阅读this API解决了问题我引用了相关部分: Eac
我想知道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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读