Here you can find the source of copy(Path source, Path target)
public static void copy(Path source, Path target) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; public class Main { public static void copy(Path source, Path target) throws IOException { if (Files.isDirectory(source)) { if (Files.isDirectory(target)) { Files.walkFileTree(source, new SimpleFileVisitor<Path>() { @Override// www . j a v a 2s . c o m public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes) throws IOException { Path relative = source.relativize(path); Path targetDir = target.resolve(relative); if (!Files.exists(targetDir)) { Files.createDirectory(targetDir); } return super.preVisitDirectory(path, basicFileAttributes); } @Override public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException { Path relative = source.relativize(path); Path targetFile = target.resolve(relative); Files.copy(path, targetFile); return super.visitFile(path, basicFileAttributes); } }); } else if (!Files.exists(target)) { Files.createDirectories(target); copy(source, target); } else { throw new IOException("Target " + target + " is a file of the directory source " + target); } } else { if (Files.isDirectory(target)) { Files.copy(source, target); } else { Files.createDirectories(target.getParent()); copy(source, target.getParent()); } } } }