MoreFiles { public static void copyDir(final Path sourceDir, final Path targetDir) throws IOException { Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path target = targetDir.resolve(sourceDir.relativize(file)); File targetDir = target.getParent().toAbsolutePath().toFile(); if (!targetDir.exists() && !targetDir.mkdirs()) { throw new IOException("can't create directory: `" + targetDir + "`"); } Files.copy(file, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); return FileVisitResult.CONTINUE; } }); }  static void delete(Path path); static Closeable watch(Path dir, EventBus eventBus,
                             ExecutorService executor, WatcherSettings watcherSettings); static void copyDir(final Path sourceDir, final Path targetDir); static void checkFileExists(String location); static void extractZip(File zip, File toDir); static String removeWindowsCarriageReturnsBeforeLF(String str); static final Function<? super Path, ? extends File> pathToFile; static final Function<String, Path> strToPath; }
@Test public void should_copy_directories() throws Exception { File source = folder.newFolder("source"); Files.write("test", new File(source, "test"), Charsets.UTF_8); File subfolder = new File(source, "subfolder"); subfolder.mkdirs(); Files.write("test2", new File(subfolder, "test2"), Charsets.UTF_8); Path targetDir = folder.getRoot().toPath().resolve("target"); MoreFiles.copyDir(source.toPath(), targetDir); assertThat(targetDir.toFile()).exists(); assertThat(targetDir.resolve("test").toFile()).exists().hasContent("test"); assertThat(targetDir.resolve("subfolder/test2").toFile()).exists().hasContent("test2"); }