Copying a directory using the SimpleFileVisitor class - Java File Path IO

Java examples for File Path IO:File Visitor

Description

Copying a directory using the SimpleFileVisitor class

Demo Code

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

public class Main {
  public static void main(String[] args) {
    try {//www .ja v  a 2 s.c  om
      Path source = Paths.get("/home");
      Path target = Paths.get("/backup");
      Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS),
          Integer.MAX_VALUE, new CopyDirectory(source, target));
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

}

class CopyDirectory extends SimpleFileVisitor<Path> {
  private Path source;
  private Path target;

  public CopyDirectory(Path source, Path target) {
    this.source = source;
    this.target = target;
  }

  @Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
      throws IOException {
    System.out.println("Copying " + source.relativize(file));
    Files.copy(file, target.resolve(source.relativize(file)));
    return FileVisitResult.CONTINUE;

  }

  @Override
  public FileVisitResult preVisitDirectory(Path directory,
      BasicFileAttributes attributes) throws IOException {
    Path targetDirectory = target.resolve(source.relativize(directory));
    try {
      System.out.println("Copying " + source.relativize(directory));
      Files.copy(directory, targetDirectory);
    } catch (FileAlreadyExistsException e) {
      if (!Files.isDirectory(targetDirectory)) {
        throw e;
      }
    }
    return FileVisitResult.CONTINUE;
  }
}

Related Tutorials