Use Files library to copy a directory tree recursively
public class CopyDirVisitor extends SimpleFileVisitor<Path> {
private Path fromPath;
private Path toPath;
private StandardCopyOption copyOption = StandardCopyOption.REPLACE_EXISTING;
....
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Path targetPath = toPath.resolve(fromPath.relativize(dir));
if(!Files.exists(targetPath)){
Files.createDirectory(targetPath);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, toPath.resolve(fromPath.relativize(file)), copyOption);
return FileVisitResult.CONTINUE;
}
}