JDK1.7 The Paths Files class implements folder copy and delete instances

  • 2020-11-25 07:17:34
  • OfStack

Examples are as follows:


public static void copyFolder(String srcFolder, String destFolder)
    throws IOException {
  long startTime = System.currentTimeMillis();
  final Path srcPath = Paths.get(srcFolder);
  //  I'm going to create more here 1 Level ii solves the problem of no shell 
  final Path destPath = Paths.get(destFolder, srcPath.toFile().getName());
  //  Check if the source folder exists 
  if (Files.notExists(srcPath)) {
    System.err.println(" The source folder does not exist ");
    System.exit(1);
  }
  //  If the target directory does not exist, create it 
  if (Files.notExists(destPath)) {
    Files.createDirectories(destPath);
  }
//  Here is the beginning of the official example, which may be parameters set for large file processing 
// Files.walkFileTree(srcPath,   EnumSet.of(FileVisitOption.FOLLOW_LINKS),
// Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {}
// A simplified opening 
  Files.walkFileTree(srcPath, new SimpleFileVisitor<Path>() {
    //  The official also calls a special folder processing, which is not used here 
    // public FileVisitResult preVisitDirectory(Path dir,
    // BasicFileAttributes attrs) throws IOException {return null;}
    @Override
    //  File processing will also folder 1 And deal with it. Make it simple 
    public FileVisitResult visitFile(Path file,
      BasicFileAttributes attrs) throws IOException {
    Path dest = destPath.resolve(srcPath.relativize(file));
    //  If the parent path does not exist, create it 
    if (Files.notExists(dest.getParent())) {
      Files.createDirectories(dest.getParent());
    }
    Files.copy(file, dest);
    return FileVisitResult.CONTINUE;
    }
  });
  long endTime = System.currentTimeMillis();
  System.out.println(" Copy success ! Time: " + (endTime - startTime) + "ms");
  }

  //  Delete folder 
  public static void deleteFolder(String Foleder) throws IOException {
  Path start = Paths.get(Foleder);
  if (Files.notExists(start)) {
    throw new IOException(" Folder does not exist! ");
  }

  Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
    @Override // Constitute the 1 An inner class 
    //  Handle file 
    public FileVisitResult visitFile(Path file,BasicFileAttributes attrs) throws IOException {
    Files.delete(file);
    return FileVisitResult.CONTINUE;
    }

    @Override
    //  Reprocessing directory 
    public FileVisitResult postVisitDirectory(Path dir, IOException e)
      throws IOException {
    if (e == null) {
      Files.delete(dir);
      return FileVisitResult.CONTINUE;
    } else {
      throw e;
    }
    }
  });
  System.out.println(" Delete successful! ");
  }

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

If you have any questions, please ask, thank you!


Related articles: