Copy Files with FileVisitor - Java File Path IO

Java examples for File Path IO:File Visitor

Description

Copy Files with FileVisitor

Demo Code

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import java.io.IOException;
import java.nio.file.FileSystemLoopException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.EnumSet;

class CopyTree implements FileVisitor<Object> {
  Path copyFrom;/*from  w  ww  .j a  v  a  2 s  .c  om*/
  Path copyTo;
  public CopyTree(Path copyFrom, Path copyTo) {
    this.copyFrom = copyFrom;
    this.copyTo = copyTo;
  }

  static void copySubTree(Path copyFrom, Path copyTo) throws IOException {
    try {
      Files.copy(copyFrom, copyTo, REPLACE_EXISTING, COPY_ATTRIBUTES);
    } catch (IOException e) {
      System.err.println("Unable to copy " + copyFrom + " [" + e + "]");
    }
  }
  @Override
  public FileVisitResult postVisitDirectory(Object dir, IOException exc)
      throws IOException {
    if (exc == null) {
      Path newdir = copyTo.resolve(copyFrom.relativize((Path) dir));
      try {
        FileTime time = Files.getLastModifiedTime((Path) dir);
        Files.setLastModifiedTime(newdir, time);
      } catch (IOException e) {
        System.err.println("Unable to copy all attributes to: " + newdir + " ["
            + e + "]");
      }
    } else {
      throw exc;
    }

    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)
      throws IOException {
    System.out.println("Copy directory: " + (Path) dir);
    Path newdir = copyTo.resolve(copyFrom.relativize((Path) dir));
    try {
      Files.copy((Path) dir, newdir, REPLACE_EXISTING, COPY_ATTRIBUTES);
    } catch (IOException e) {
      System.err.println("Unable to create " + newdir + " [" + e + "]");
      return FileVisitResult.SKIP_SUBTREE;
    }

    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)
      throws IOException {
    System.out.println("Copy file: " + (Path) file);
    copySubTree((Path) file, copyTo.resolve(copyFrom.relativize((Path) file)));
    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult visitFileFailed(Object file, IOException exc)
      throws IOException {
    if (exc instanceof FileSystemLoopException) {
      System.err.println("Cycle was detected: " + (Path) file);
    } else {
      System.err.println("Error occured, unable to copy:" + (Path) file + " ["
          + exc + "]");
    }
    return FileVisitResult.CONTINUE;
  }
}

class Main {
  public static void main(String[] args) throws IOException {
    Path copyFrom = Paths.get("C:/folder1");
    Path copyTo = Paths.get("C:/folder1_copy");
    CopyTree walk = new CopyTree(copyFrom, copyTo);
    EnumSet opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    Files.walkFileTree(copyFrom, opts, Integer.MAX_VALUE, walk);
  }
}

Related Tutorials