Java有几种文件拷贝方式?哪一种最高效?

Java有几种文件拷贝方式?哪一种最高效?

典型回答

1.利用java.io类库,直接为源文件构建一个FileInputStream读取,然后再为目标文件构建一个FileOutputStream,完成写入工作

public satic void copyFileByStream(File source, File des) throws IOException {
    try (InputStream is = new FileInputStream(source);
         OutputStream os = new FileOutputStream(des);){
        byte[] bufer = new byte[1024];
        int length;
        while ((length = is.read(bufer)) > 0) {
           os.write(bufer, 0, length);
        }
    }
}

2.利用java.nio类库提供的transferTo或transferFrom方法实现。

public satic void copyFileByChannel(File source, File des) throws IOException {
  try (FileChannel sourceChannel = new FileInputStream(source).getChannel();
       FileChannel targetChannel = new FileOutputStream(des).getChannel();){
      for (long count = sourceChannel.size() ;count>0 ;) {
          long transferred = sourceChannel.transferTo(sourceChannel.position(), count, targetChannel); 
          sourceChannel.position(sourceChannel.position() + transferred);
          count -= transferred;
      }
  }
}

private static void copyFileUsingFileChannels(File source, File dest) throws IOException {    
       FileChannel inputChannel = null;    
       FileChannel outputChannel = null;    
   try {
       inputChannel = new FileInputStream(source).getChannel();
       outputChannel = new FileOutputStream(dest).getChannel();
       outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
   } finally {
       inputChannel.close();
       outputChannel.close();
   }
}

3.Java标准类库本身已经提供了几种Files.copy的实现

private static void copyFileUsingJava7Files(File source, File dest)
        throws IOException {    
        Files.copy(source.toPath(), dest.toPath());
}

对于Copy的效率,这个其实与操作系统和配置等情况相关,总体上来说,NIO transferTo/From的方式可能更快,因为它更能利用现代操作系统底层机制,避免不必要拷贝和上下
文切换。

拷贝实现机制分析

首先,你需要理解用户态空间(User Space)和内核态空间(Kernel Space),这是操作系统层面的基本概念,操作系统内核、硬件驱动等运行在内核态空间,具有相对高的特
权;而用户态空间,则是给普通应用和服务使用。你可以参考:https://en.wikipedia.org/wiki/User_space。

当我们使用输入输出流进行读写时,实际上是进行了多次上下文切换,比如应用读取数据时,先在内核态将数据从磁盘读取到内核缓存,再切换到用户态将数据从内核缓存读取到用
户缓存。

写入操作也是类似,仅仅是步骤相反,你可以参考下面这张图。
在这里插入图片描述

所以,这种方式会带来一定的额外开销,可能会降低IO效率。

而基于NIO transferTo的实现方式,在Linux和Unix上,则会使用到零拷贝技术,数据传输并不需要用户态参与,省去了上下文切换的开销和不必要的内存拷贝,进而可能提高应用
拷贝性能。注意,transferTo不仅仅是可以用在文件拷贝中,与其类似的,例如读取磁盘文件,然后进行Socket发送,同样可以享受这种机制带来的性能和扩展性提高。

transferTo的传输过程是:
在这里插入图片描述

更多资源关注公众号BatFor

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值