java filewriter的用法,java - FileWriter是否使用缓冲区? (它的行为就像我的例子中那样)...

I am using FileWriter and I have noticed strange behavior.

I buffer my collection myself and every x rows I use

IOUtils.writelines(myList,"\n", writer );

It doesnt write to the file. I continue to call it with more lines and only after it is very full it writes to the file.

Does it use a buffer? I cant find it in its documentation.

解决方案

The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.

(My emphasis)

So clearly it's buffered (unless the default byte-buffer size is zero and they're being really odd with their phrasing).

I suspect it's just using an OutputStreamWriter on a FileOutputStream. Looking at OutputStreamWriter:

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.

(My emphasis)

If you want to ensure that various buffers at various levels are flushed, to the extent you can, look at using the flush method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值