Heim  >  Artikel  >  Java  >  Kopieren von Dateien mithilfe indirekter Java NIO-Puffer

Kopieren von Dateien mithilfe indirekter Java NIO-Puffer

WBOY
WBOYnach vorne
2023-05-07 11:19:07986Durchsuche

Dateien über den indirekten JAVA NIO-Puffer kopieren

  /**
   * 通过JAVA NIO 非直接缓冲区拷贝文件
   *
   * @param sourcePath 源文件路径
   * @param targetPath 目标文件路径
   */
  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();

      //分配指定大小的缓冲区
      ByteBuffer buf = ByteBuffer.allocate(1024);

      while (inChannel.read(buf) != -1) {
        //转换为读取数据模式
        buf.flip();
        //写入到磁盘
        outChannel.write(buf);
        //清空缓冲区
        buf.clear();
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      //关闭流
      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();
      }
    }
  }

Das obige ist der detaillierte Inhalt vonKopieren von Dateien mithilfe indirekter Java NIO-Puffer. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:yisu.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen