K0NRAD
5/23/2014 - 10:57 AM

copy path recursivly with NIO

copy path recursivly with NIO

    public void copyPath(Path source, Path target) throws IOException {
        Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                return copy(file);
            }

            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return copy(dir);
            }

            private FileVisitResult copy(Path fileOrDir) throws IOException {
                Files.copy(fileOrDir, target.resolve(source.relativize(fileOrDir)), StandardCopyOption.REPLACE_EXISTING);
                return FileVisitResult.CONTINUE;
            }
        });
    }