溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何通過JAVA NIO通道傳輸拷貝文件

發(fā)布時(shí)間:2022-03-31 13:59:18 來源:億速云 閱讀:328 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)如何通過JAVA NIO通道傳輸拷貝文件的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

通過JAVA NIO 通道傳輸拷貝文件

方式一

 /**
   * 通過JAVA NIO 通道傳輸拷貝文件
   *
   * @param sourcePath 源文件路徑
   * @param targetPath 目標(biāo)文件路徑
   */
  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 {
      //關(guān)閉流
      try {
        if (outChannel != null) {
          outChannel.close();
        }
        if (inChannel != null) {
          inChannel.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

方式二

 /**
   * 通過JAVA NIO 通道傳輸拷貝文件
   *
   * @param sourcePath 源文件路徑
   * @param targetPath 目標(biāo)文件路徑
   */
  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 {
      //關(guān)閉流
      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("通過字節(jié)流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time1));

    long time2 = System.currentTimeMillis();
    copyFileByReaderAndWriter(source, target + "2.txt");
    System.out.println("通過字符流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time2));

    long time3 = System.currentTimeMillis();
    copyFileByBuffered(source, target + "3.txt");
    System.out.println("通過字節(jié)緩沖流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time3));

    long time4 = System.currentTimeMillis();
    copyFileByBufferedChar(source, target + "4.txt");
    System.out.println("通過字符緩沖流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time4));

    long time5 = System.currentTimeMillis();
    copyFileByChannel(source, target + "5.txt");
    System.out.println("通過JAVA NIO通道(非直接緩沖區(qū))實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time5));

    long time6 = System.currentTimeMillis();
    copyFileByChannelBufferd(source, target + "6.txt");
    System.out.println("通過JAVA NIO通道(直接緩沖區(qū))實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time6));

    long time7 = System.currentTimeMillis();
    copyFileByChannelTransfer(source, target + "7.txt");
    System.out.println("通過JAVA NIO通道傳輸實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time7));

    long time8 = System.currentTimeMillis();
    copyFileByChannelTransfer(source, target + "8.txt");
    System.out.println("通過JAVA NIO通道傳輸2實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time8));

感謝各位的閱讀!關(guān)于“如何通過JAVA NIO通道傳輸拷貝文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI