FileChannel中transfer大文件问题
问题
当使用 FileChannel.transfer保存超大文件时,如果超过2G,在windows下可能最多保存成功2G,所以需要循环transferTo
@Slf4j
public class FileTest {
/**
* 最大只能复制2G
*/
@Test
public void fileCopy() {
try (FileChannel rc = FileChannel.open(Paths.get("E:\\origin\\", "test.iso"), StandardOpenOption.READ);
FileChannel wc = new FileOutputStream(new File("E:\\origin\\aa.iso")).getChannel()) {
//原始最大转换2G
// rc.transferTo(0, rc.size(), wc);
transferTo(rc, wc);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
循环transferTo
**/
public void transferTo(FileChannel from, FileChannel to) throws IOException {
long total = from.size();
long left = total;
while (left > 0) {
long pos = total - left;
log.info("position:{},left:{}", pos, left);
left -= from.transferTo(pos, left, to);
}
}
}