最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實現(xiàn)文件拷貝的七種方式

 更新時間:2020年02月04日 15:34:27   作者:zhaojiaxing0216  
這篇文章主要介紹了java實現(xiàn)文件拷貝的七種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1. 通過字節(jié)流實現(xiàn)文件的拷貝

 /**
   * 通過字節(jié)流實現(xiàn)文件的拷貝
   * @param sourcePath 源文件路徑
   * @param targetPath 目標(biāo)文件路徑
   */
  public static void copyFileByStream(String sourcePath,String targetPath){
    //源文件路徑
    File source = new File(sourcePath);
    //目標(biāo)文件路徑
    File target = new File(targetPath);

    //如果源文件不存在則不能拷貝
    if(!source.exists()){
      return;
    }
    //如果目標(biāo)文件目錄不存在則創(chuàng)建
    if(!target.getParentFile().exists()){
      target.getParentFile().mkdirs();
    }

    try {
      //實現(xiàn)文件的拷貝
      InputStream inputStream = new FileInputStream(source);
      OutputStream outputStream = new FileOutputStream(target);
      int temp = 0;
      //每次讀取1024個字節(jié)
      byte[] data = new byte[1024];
      //將每次讀取的數(shù)據(jù)保存到字節(jié)數(shù)組里面,并且返回讀取的個數(shù)
      while ((temp = inputStream.read(data)) != -1){
        //輸出數(shù)組
        outputStream.write(data,0,temp);
      }

      inputStream.close();
      outputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

2. 通過字符流實現(xiàn)文件拷貝

使用字符流只能拷貝文本文件

  /**
   * 通過字符流實現(xiàn)文件的拷貝
   *
   * @param sourcePath 源文件路徑
   * @param targetPath 目標(biāo)文件路徑
   */
  public static void copyFileByReaderAndWriter(String sourcePath, String targetPath) {
    //源文件路徑
    File source = new File(sourcePath);
    //目標(biāo)文件路徑
    File target = new File(targetPath);

    //如果源文件不存在則不能拷貝
    if (!source.exists()) {
      return;
    }
    //如果目標(biāo)文件目錄不存在則創(chuàng)建
    if (!target.getParentFile().exists()) {
      target.getParentFile().mkdirs();
    }

    FileReader in = null;
    FileWriter out = null;
    try {
      //字符輸入流和字符輸出流
      in = new FileReader(source);
      out = new FileWriter(target);

      char[] c = new char[1024];
      int temp = 0;
      //每次讀取1024個字符
      while ((temp = in.read(c)) != -1) {
        //輸出到文件
        out.write(c, 0, temp);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      //關(guān)閉流
      try {
        if (in != null) {
          in.close();
        }
        if (out != null) {
          out.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

3. 通過字節(jié)緩沖流實現(xiàn)文件拷貝

/**
   * 通過字節(jié)緩沖流實現(xiàn)文件的拷貝
   *
   * @param sourcePath 源文件路徑
   * @param targetPath 目標(biāo)文件路徑
   */
  public static void copyFileByBuffered(String sourcePath, String targetPath){
    //源文件路徑
    File source = new File(sourcePath);
    //目標(biāo)文件路徑
    File target = new File(targetPath);

    //如果源文件不存在則不能拷貝
    if (!source.exists()) {
      return;
    }
    //如果目標(biāo)文件目錄不存在則創(chuàng)建
    if (!target.getParentFile().exists()) {
      target.getParentFile().mkdirs();
    }

    InputStream in = null;
    OutputStream out = null;
    try {
      //字節(jié)緩沖輸入流和字節(jié)緩沖輸出流
      in = new BufferedInputStream(new FileInputStream(source));
      out = new BufferedOutputStream(new FileOutputStream(target));

      byte[] b = new byte[1024];
      int temp = 0;
      //每次讀取一個1024的字節(jié)數(shù)組
      while((temp = in.read(b)) != -1){
        //輸出到文件
        out.write(b,0,temp);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      //關(guān)閉流
      try {
        if (in != null) {
          in.close();
        }
        if (out != null) {
          out.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

4. 通過字符緩沖流拷貝文件

字符緩沖流只能讀取文本文件

 /**
   * 通過字符緩沖流實現(xiàn)文件的拷貝
   *
   * @param sourcePath 源文件路徑
   * @param targetPath 目標(biāo)文件路徑
   */
  public static void copyFileByBufferedChar(String sourcePath, String targetPath){
    //源文件路徑
    File source = new File(sourcePath);
    //目標(biāo)文件路徑
    File target = new File(targetPath);

    //如果源文件不存在則不能拷貝
    if (!source.exists()) {
      return;
    }
    //如果目標(biāo)文件目錄不存在則創(chuàng)建
    if (!target.getParentFile().exists()) {
      target.getParentFile().mkdirs();
    }

    BufferedReader in = null;
    BufferedWriter out = null;

    try {
      //字符緩沖輸入流和字符緩沖輸出流
      in = new BufferedReader(new FileReader(source));
      out = new BufferedWriter(new FileWriter(target));

      //讀取文件(每次讀取一行)
      String temp = null;
      while((temp = in.readLine()) != null){
        //輸出到文件
        out.write(temp);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      //關(guān)閉流
      try {
        if (in != null) {
          in.close();
        }
        if (out != null) {
          out.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

5. 通過JAVA NIO 非直接緩沖區(qū)拷貝文件

  /**
   * 通過JAVA NIO 非直接緩沖區(qū)拷貝文件
   *
   * @param sourcePath 源文件路徑
   * @param targetPath 目標(biāo)文件路徑
   */
  public static void copyFileByChannel(String sourcePath, String targetPath) {
    FileChannel outChannel = null;
    FileChannel inChannel = null;

    FileInputStream fis = null;
    FileOutputStream fos = null;

    try {
      fis = new FileInputStream(sourcePath);
      fos = new FileOutputStream(targetPath);

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

      //分配指定大小的緩沖區(qū)
      ByteBuffer buf = ByteBuffer.allocate(1024);

      while (inChannel.read(buf) != -1) {
        //轉(zhuǎn)換為讀取數(shù)據(jù)模式
        buf.flip();
        //寫入到磁盤
        outChannel.write(buf);
        //清空緩沖區(qū)
        buf.clear();
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      //關(guān)閉流
      try {
        if (outChannel != null) {
          outChannel.close();
        }
        if (inChannel != null) {
          inChannel.close();
        }
        if (fis != null) {
          fis.close();
        }
        if (fos != null) {
          fos.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

6. 通過JAVA NIO 直接緩沖區(qū)拷貝文件

/**
   * 通過JAVA NIO 直接緩沖區(qū)拷貝文件(內(nèi)存映射文件)
   *
   * @param sourcePath 源文件路徑
   * @param targetPath 目標(biāo)文件路徑
   */
  public static void copyFileByChannelBufferd(String sourcePath, String targetPath) {
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
      //獲取通道,StandardOpenOption.READ表示可讀,StandardOpenOption.WRITE表示可寫,StandardOpenOption.CREATE表示可以創(chuàng)建
      inChannel = FileChannel.open(Paths.get(sourcePath), StandardOpenOption.READ);
      outChannel = FileChannel.open(Paths.get(targetPath), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);

      //創(chuàng)建內(nèi)存映射文件
      MappedByteBuffer inMapped = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
      MappedByteBuffer outMapped = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());

      //直接操作內(nèi)存映射文件
      byte[] buf = new byte[inMapped.limit()];
      inMapped.get(buf);
      outMapped.put(buf);

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      //關(guān)閉流
      try {
        if (outChannel != null) {
          outChannel.close();
        }
        if (inChannel != null) {
          inChannel.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

7. 通過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é)流實現(xiàn)文件的拷貝耗時:" + (System.currentTimeMillis() - time1));

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

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

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

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

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

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

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

通過測試發(fā)現(xiàn),使用JAVA NIO通道傳輸、JAVA NIO通道直接緩沖區(qū)以及字節(jié)緩沖流拷貝文件效率最高

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java序列化常見實現(xiàn)方法代碼實例

    Java序列化常見實現(xiàn)方法代碼實例

    這篇文章主要介紹了Java序列化常見實現(xiàn)方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Mybatis批量插入返回成功的數(shù)目實例

    Mybatis批量插入返回成功的數(shù)目實例

    這篇文章主要介紹了Mybatis批量插入返回成功的數(shù)目實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Java內(nèi)存模型JMM詳解

    Java內(nèi)存模型JMM詳解

    這篇文章主要介紹了Java內(nèi)存模型JMM詳解,涉及volatile和監(jiān)視器鎖,final字段,內(nèi)存屏障等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • 如何在spring官網(wǎng)查找XML基礎(chǔ)配置文件

    如何在spring官網(wǎng)查找XML基礎(chǔ)配置文件

    這篇文章主要介紹了如何在spring官網(wǎng)查找XML基礎(chǔ)配置文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot啟動報錯的11個高頻問題排查與解決終極指南

    SpringBoot啟動報錯的11個高頻問題排查與解決終極指南

    這篇文章主要為大家詳細介紹了SpringBoot啟動報錯的11個高頻問題的排查與解決,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2025-03-03
  • Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問題

    Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問題

    跨站腳本攻擊XSS是最普遍的Web應(yīng)用安全漏洞,本文主要介紹了Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問題,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • JAVA Comparator 和 Comparable接口使用方法

    JAVA Comparator 和 Comparable接口使用方法

    本文介紹了Java中Comparable和Comparator接口的使用,包括它們的定義、方法和應(yīng)用場景,Comparable用于定義類的自然排序規(guī)則,而Comparator提供了一種靈活的方式來定義對象之間的排序規(guī)則,無需修改類本身,感興趣的朋友一起看看吧
    2025-03-03
  • Spring MVC請求參數(shù)接收的全面總結(jié)教程

    Spring MVC請求參數(shù)接收的全面總結(jié)教程

    這篇文章主要給大家總結(jié)介紹了關(guān)于Spring MVC請求參數(shù)接收的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • 一文精通Java 多線程之全方位解讀

    一文精通Java 多線程之全方位解讀

    Java 給多線程編程提供了內(nèi)置的支持。 一條線程指的是進程中一個單一順序的控制流,一個進程中可以并發(fā)多個線程,每條線程并行執(zhí)行不同的任務(wù),多線程是多任務(wù)的一種特別的形式,但多線程使用了更小的資源開銷
    2021-10-10
  • Java動態(tài)代理的應(yīng)用詳解

    Java動態(tài)代理的應(yīng)用詳解

    本篇文章介紹了,Java動態(tài)代理的應(yīng)用詳解,需要的朋友參考下
    2013-05-05

最新評論

措美县| 酒泉市| 平利县| 聂拉木县| 芮城县| 兴海县| 信宜市| 葫芦岛市| 舒兰市| 霍邱县| 东丽区| 奈曼旗| 建始县| 桑日县| 五华县| 张家界市| 获嘉县| 高州市| 喀喇沁旗| 耒阳市| 乐安县| 周口市| 湖州市| 临沧市| 沅江市| 西乡县| 万州区| 敖汉旗| 永春县| 苗栗市| 堆龙德庆县| 乌拉特前旗| 渭源县| 澳门| 永丰县| 克东县| 娱乐| 玉田县| 萝北县| 上虞市| 灵丘县|