Copying a directory using the SimpleFileVisitor class : SimpleFileVisitor « JDK 7 « Java

Copying a directory using the SimpleFileVisitor class

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 Test {

  public static void main(String[] args) {
    try {
      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) {

class CopyDirectory extends SimpleFileVisitor<Path> {

  private Path source;
  private Path target;

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

  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;

  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 examples in the same category

1.List all Java file recursively with SimpleFileVisitor
2.Deleting a directory using the SimpleFileVisitor class
3.Using the SimpleFileVisitor class to traverse file systems