> Java > java지도 시간 > 본문

JAVA NIO 채널을 통해 복사된 파일을 전송하는 방법

王林
풀어 주다: 2023-05-15 18:16:06
앞으로
1716명이 탐색했습니다.

通过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 채널을 통해 복사된 파일을 전송하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!