Java写文件的缓冲区有多大_java – 将FileChannel#write总是写入整个缓冲区...

(这与Would FileChannel.read read less bytes than specified if there’s enough data?相关(或更确切地说是“相反”))

TL; DR:

这总是会写出整个缓冲区……

ByteBuffer bytes = ...;

fileOutputStream.getChannel().write(bytes);

…或者是否有必要使用这样的循环:

ByteBuffer bytes = ...;

while (bytes.remaining() > 0)

{

fileOutputStream.getChannel().write(bytes);

}

仅供参考:文档说明

Writes a sequence of bytes to this channel from the given buffer.

Bytes are written starting at this channel’s current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified by the WritableByteChannel interface.

Writes a sequence of bytes to this channel from the given buffer.

An attempt is made to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, src.remaining(), at the moment this method is invoked.

Suppose that a byte sequence of length n is written, where 0 <= n <= r. This byte sequence will be transferred from the buffer starting at index p, where p is the buffer’s position at the moment this method is invoked; the index of the last byte written will be p + n – 1. Upon return the buffer’s position will be equal to p + n; its limit will not have changed.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket’s output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters:

src – The buffer from which bytes are to be retrieved

Returns: The number of bytes written, possibly zero

在上面提到的关于从FileChannel阅读的问题中,在评论中已经对该文档的确切措辞和解释进行了一些讨论.我认为文档中的关键区别在于对于read方法,文档说

A read operation might not fill the buffer, and in fact it might not read any bytes at all.

与此相反,write方法的文档说

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all.

对我来说,这意味着FileChannel上的写操作只会在写完所有字节后返回,因为文档中没有另外指定(除了返回值可能为0的语句,但这显然是一个工件)从被覆盖的方法)

从我的文件大小高达80 MB(!)的测试中,写操作总是立即写入整个缓冲区.但当然,这只是一个考验,并不足以作出深刻的陈述.我试图跟踪related OpenJDK classes中的调用,但这些调用很快就会分散到不同的本机实现中 – 毕竟,这不应该是必要的……

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值