其他分享
首页 > 其他分享> > 4.10 NIO中零拷贝原理

4.10 NIO中零拷贝原理

作者:互联网

4.10 零拷贝

       零拷贝即Zero-Copy,顾名思义,零拷贝是指的一种非拷贝的方式来减少IO次数的工作方式。零拷贝的作用就是减少IO,提高IO效率。


4.10.1 传统IO方式

传统IO读写方式
       传统IO的工作方式需要经历多次文件拷贝,还需要程序在操作系统模式和用户模式之间来回切换,这样来回一圈后才来完成一次文件修改,这样极大浪费了内存,效率偏低,所以在传统IO的基础上有了零拷贝的方式。

4.10.2 零拷贝原理

在这里插入图片描述

4.10.3 Java中如何使用零拷贝

       使用Java中RandomAccessFile的transferTo方法或者transferFrom方法,具体实例如下。

public static void main(String[] args) throws IOException {
    RandomAccessFile from
            = new RandomAccessFile("src/main/java/com/lbh/nio/from.txt", "rw");
    RandomAccessFile to
            = new RandomAccessFile("src/main/java/com/lbh/nio/to.txt", "rw");
    FileChannel channel = from.getChannel();
    FileChannel toChannel = to.getChannel();
    channel.transferTo(0,channel.size(),toChannel);
}

标签:4.10,NIO,RandomAccessFile,中零,IO,拷贝,main,channel
来源: https://blog.csdn.net/weixin_44671737/article/details/115025603