一些互联网文档向我展示了示例,
FileChannel target;
FileChannel source;
target.trasferFrom(source, 0, source.size()); // done!
或
FileChannel source;
FileChannel target;
source.transferTo(0, source.size(), target); // done!
但文件说这些方法实际上可以转移少于给定的。
transferFrom
和transferTo
,如果source
或target
是FileChannel
的实例,请转移所有给定的计数?
我不应该像这样循环吗?
for (long count = Files.size(source); count > 0L;) {
final long transferred = readable.transferTo(
readable.position(), count, writable);
readable.position(readable.position() + transferred);
count -= transferred;
}
for (long count = Files.size(source); count > 0L;) {
final long transferred = writable.transferFrom(
readable, writable.position(), count);
writable.position(writable.position() + transferred);
count -= transferred;
}
答案 0 :(得分:2)
是的,文档是正确的,你必须循环。