如何通过JAVANIO通道传输拷贝文件

这篇文章给大家分享的是有关如何通过JAVA NIO通道传输拷贝文件的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

成都创新互联公司是专业的西吉网站建设公司,西吉接单;提供成都做网站、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行西吉网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

通过JAVA NIO 通道传输拷贝文件

方式一

 /**
   * 通过JAVA NIO 通道传输拷贝文件
   *
   * @param sourcePath 源文件路径
   * @param targetPath 目标文件路径
   */
  public static void copyFileByChannelTransfer(String sourcePath, String targetPath) {
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
      //获取通道
      inChannel = FileChannel.open(Paths.get(sourcePath), StandardOpenOption.READ);
      outChannel = FileChannel.open(Paths.get(targetPath),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);

      inChannel.transferTo(0,inChannel.size(),outChannel);
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      //关闭流
      try {
        if (outChannel != null) {
          outChannel.close();
        }
        if (inChannel != null) {
          inChannel.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

方式二

 /**
   * 通过JAVA NIO 通道传输拷贝文件
   *
   * @param sourcePath 源文件路径
   * @param targetPath 目标文件路径
   */
  public static void copyFileByChannelTransfer2(String sourcePath, String targetPath) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
      fis = new FileInputStream(sourcePath);
      fos = new FileOutputStream(targetPath);

      //获取通道
      inChannel = fis.getChannel();
      outChannel = fos.getChannel();

      inChannel.transferTo(0,inChannel.size(),outChannel);
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      //关闭流
      try {
        if (outChannel != null) {
          outChannel.close();
        }
        if (inChannel != null) {
          inChannel.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

使用示例

String source = "e:\\demo\\纵天神帝.txt";
    String target = "e:\\demo\\";
    long time1 = System.currentTimeMillis();
    copyFileByStream(source, target + "1.txt");
    System.out.println("通过字节流实现文件的拷贝耗时:" + (System.currentTimeMillis() - time1));

    long time2 = System.currentTimeMillis();
    copyFileByReaderAndWriter(source, target + "2.txt");
    System.out.println("通过字符流实现文件的拷贝耗时:" + (System.currentTimeMillis() - time2));

    long time3 = System.currentTimeMillis();
    copyFileByBuffered(source, target + "3.txt");
    System.out.println("通过字节缓冲流实现文件的拷贝耗时:" + (System.currentTimeMillis() - time3));

    long time4 = System.currentTimeMillis();
    copyFileByBufferedChar(source, target + "4.txt");
    System.out.println("通过字符缓冲流实现文件的拷贝耗时:" + (System.currentTimeMillis() - time4));

    long time5 = System.currentTimeMillis();
    copyFileByChannel(source, target + "5.txt");
    System.out.println("通过JAVA NIO通道(非直接缓冲区)实现文件的拷贝耗时:" + (System.currentTimeMillis() - time5));

    long time6 = System.currentTimeMillis();
    copyFileByChannelBufferd(source, target + "6.txt");
    System.out.println("通过JAVA NIO通道(直接缓冲区)实现文件的拷贝耗时:" + (System.currentTimeMillis() - time6));

    long time7 = System.currentTimeMillis();
    copyFileByChannelTransfer(source, target + "7.txt");
    System.out.println("通过JAVA NIO通道传输实现文件的拷贝耗时:" + (System.currentTimeMillis() - time7));

    long time8 = System.currentTimeMillis();
    copyFileByChannelTransfer(source, target + "8.txt");
    System.out.println("通过JAVA NIO通道传输2实现文件的拷贝耗时:" + (System.currentTimeMillis() - time8));

感谢各位的阅读!关于“如何通过JAVA NIO通道传输拷贝文件”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

网站题目:如何通过JAVANIO通道传输拷贝文件
当前链接:https://www.cdcxhl.com/article26/igsecg.html

成都网站建设公司_创新互联,为您提供网站建设做网站网站营销搜索引擎优化企业建站域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站建设