【从NIO到Netty】7.零拷贝案例1-复制文件

这段代码使用零拷贝复制了一个 1.37GB的文件,在我的电脑上耗时大概有10秒左右

public class NioZeroCopy {
    public static void main(String[] args) throws IOException {
        File file1 = new File("E:/我的电影/科幻片/狼之歌.mkv");
        File file2 = new File("E:/我的电影/科幻片/狼之歌-copy.mkv");
        FileChannel channel = new FileInputStream(file1).getChannel();
        channel.transferTo(0, file1.length(), new FileOutputStream(file2).getChannel());
    }
}

transferTo方法,会在OS支持的情况下使用零拷贝来实现,这一点在其注释中有说明

猜你喜欢

转载自www.cnblogs.com/heben/p/13203677.html