溫馨提示×

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

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

JDK1.7 Paths,Files類實(shí)現(xiàn)文件夾的復(fù)制與刪除的實(shí)例

發(fā)布時(shí)間:2020-09-19 09:50:31 來源:腳本之家 閱讀:200 作者:尚云峰111 欄目:編程語(yǔ)言

實(shí)例如下所示:

public static void copyFolder(String srcFolder, String destFolder)
    throws IOException {
  long startTime = System.currentTimeMillis();
  final Path srcPath = Paths.get(srcFolder);
  // 這里多創(chuàng)建一級(jí),就解決了沒有外殼的問題
  final Path destPath = Paths.get(destFolder, srcPath.toFile().getName());
  // 檢查源文件夾是否存在
  if (Files.notExists(srcPath)) {
    System.err.println("源文件夾不存在");
    System.exit(1);
  }
  // 如果目標(biāo)目錄不存在,則創(chuàng)建
  if (Files.notExists(destPath)) {
    Files.createDirectories(destPath);
  }
// 這里是官方例子的開頭,可能是針對(duì)大文件處理設(shè)置的參數(shù)
// Files.walkFileTree(srcPath,   EnumSet.of(FileVisitOption.FOLLOW_LINKS),
// Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {}
//簡(jiǎn)化后的開頭
  Files.walkFileTree(srcPath, new SimpleFileVisitor<Path>() {
    // 官方還調(diào)用了專門的文件夾處理,這里沒使用
    // public FileVisitResult preVisitDirectory(Path dir,
    // BasicFileAttributes attrs) throws IOException {return null;}
    @Override
    // 文件處理,將文件夾也一并處理,簡(jiǎn)潔些
    public FileVisitResult visitFile(Path file,
      BasicFileAttributes attrs) throws IOException {
    Path dest = destPath.resolve(srcPath.relativize(file));
    // 如果說父路徑不存在,則創(chuàng)建
    if (Files.notExists(dest.getParent())) {
      Files.createDirectories(dest.getParent());
    }
    Files.copy(file, dest);
    return FileVisitResult.CONTINUE;
    }
  });
  long endTime = System.currentTimeMillis();
  System.out.println("復(fù)制成功!耗時(shí):" + (endTime - startTime) + "ms");
  }

  // 刪除文件夾
  public static void deleteFolder(String Foleder) throws IOException {
  Path start = Paths.get(Foleder);
  if (Files.notExists(start)) {
    throw new IOException("文件夾不存在!");
  }

  Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
    @Override //構(gòu)成了一個(gè)內(nèi)部類
    // 處理文件
    public FileVisitResult visitFile(Path file,BasicFileAttributes attrs) throws IOException {
    Files.delete(file);
    return FileVisitResult.CONTINUE;
    }

    @Override
    // 再處理目錄
    public FileVisitResult postVisitDirectory(Path dir, IOException e)
      throws IOException {
    if (e == null) {
      Files.delete(dir);
      return FileVisitResult.CONTINUE;
    } else {
      throw e;
    }
    }
  });
  System.out.println("刪除成功!");
  }

  public static void main(String[] args) throws IOException {
//copyFolder("C:\\Users\\Administrator\\Desktop\\111", "D:\\壓縮\\1級(jí)\\2級(jí)");// 419ms,378ms,429ms....
deleteFolder("C:\\Users\\Administrator\\Desktop\\111");}

如有問題,還請(qǐng)?zhí)岢?,謝謝!

以上這篇JDK1.7 Paths,Files類實(shí)現(xiàn)文件夾的復(fù)制與刪除的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向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