MoreFiles { public static void copyRecursive(Path source, Path target) throws IOException { if (Files.exists(target)) { throw new FileAlreadyExistsException(target.toString()); } if (!Files.exists(target.getParent())) { throw new NoSuchFileException(target.toString()); } Path normalizedSource = source.normalize().toAbsolutePath(); Path normalizedTarget = target.normalize().toAbsolutePath(); if (normalizedTarget.startsWith(normalizedSource)) { throw new FileSystemException( normalizedSource.toString(), normalizedTarget.toString(), "cannot copy a path into itself"); } FileSystemProvider sourceProvider = source.getFileSystem().provider(); FileSystemProvider targetProvider = target.getFileSystem().provider(); if (sourceProvider instanceof RecursiveCopy) { try { ((RecursiveCopy) sourceProvider).copyRecursive(source, target); return; } catch (UnsupportedOperationException e) { } } if (targetProvider instanceof RecursiveCopy) { try { ((RecursiveCopy) targetProvider).copyRecursive(source, target); return; } catch (UnsupportedOperationException e) { } } Files.walkFileTree(source, new CopyVisitor(source, target)); } private  MoreFiles(); static DirectoryStream.Filter<Path> regularFileFilter(); static DirectoryStream.Filter<Path> directoryFilter(); static void deleteRecursive(Path path); static boolean deleteRecursiveIfExists(Path path); static void copyLarge(Path source, Path target); static void copyRecursive(Path source, Path target); static String readAllString(Path path, Charset cs); static Path write(Path path, String string, Charset cs, OpenOption... options); static Path defaultDirectory(FileSystem fs); static void addPermission(Path path, PosixFilePermission permission); static void addPermissionRecursive(Path path, PosixFilePermission permission); static void setExecutable(Path path); static void removePermission(Path path, PosixFilePermission permission); static void removePermissionRecursive(Path path, PosixFilePermission permission); static void setPermissionRecursive(Path path, Set<PosixFilePermission> permissions); static boolean isEmpty(Path path); static List<Path> listDirectory(Path dir); static List<Path> listDirectoryFiles(Path dir); static List<Path> listDirectoryRecursive(Path dir); static List<Path> listDirectoryFilesRecursive(Path dir); static boolean isLocal(Path path);  }
@Test(expected = FileAlreadyExistsException.class) public void copyRecursiveFailsIfTargetExists() throws IOException { Path source = workingDir.newFile("source").toPath(); Path target = workingDir.newFile("target").toPath(); MoreFiles.copyRecursive(source, target); }
@Test(expected = NoSuchFileException.class) public void copyRecursiveFailsIfTargetParentMissing() throws IOException { Path source = workingDir.newFile("source").toPath(); Path parent = workingDir.getRoot().toPath().resolve("parent"); MoreFiles.copyRecursive(source, parent.resolve("target")); }
@Test(expected = FileSystemException.class) public void copyRecursiveFailsIfTargetInsideSource() throws IOException { Path source = workingDir.newFile("source").toPath(); Path target = source.resolve("target").relativize(source); MoreFiles.copyRecursive(source, target); }