import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class FileChannelTest { public static void main(String[] args) { try ( FileChannel fromChannel = new FileInputStream("files/text.txt").getChannel(); FileChannel toChannel = new FileOutputStream("files/textTo.txt").getChannel(); ) { // 效率高,底层会利用操作系统的零拷贝进行优化,上限是 2g 数据 //fromChannel.transferTo(0, fromChannel.size(), toChannel); long size = fromChannel.size(); // left 表示剩余多少字节 // transferTo() 方法会返回实际传输的字节数,可能为0 for (long left = size; left > 0; ) { left -= fromChannel.transferTo((size - left), left, toChannel); } } catch (IOException e) { e.printStackTrace(); } } }
标签:java,FileChannel,fromChannel,import,数据传输,拷贝,left,size From: https://www.cnblogs.com/zhousjcn/p/16789606.html