Here you can find the source of copyTree(final Path source, final Path target)
public static void copyTree(final Path source, final Path target) throws IOException
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class Main { /**//from ww w . j a va 2s . c om * Recursively copy the contents of source into the target; target does not have to exist, it can * exist. Does not follow the symbolic links. */ public static void copyTree(final Path source, final Path target) throws IOException { if (!Files.exists(source)) { throw new IOException(String.format("Can not copy: source directory %s does not exist", source.toAbsolutePath().toString())); } Files.createDirectories(target); Files.walkFileTree(source, new SimpleFileVisitor<Path>() { Path currentTarget = target; @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (!source.equals(dir)) { currentTarget = currentTarget.resolve(dir.getFileName().toString()); Files.createDirectories(currentTarget); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.copy(file, currentTarget.resolve(file.getFileName().toString())); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) { currentTarget = currentTarget.getParent(); return FileVisitResult.CONTINUE; } }); } /** * Resolves the Path to the file or directory under the <code> * directory/parts[0]/parts[1]/.../parts[n]</code>. * * @param directory root directory for resolve * @param parts parts of the path relative to directory * @return resolved Path */ public static Path resolve(Path directory, String... parts) { Path current = directory; for (String part : parts) { current = current.resolve(part); } return current; } }