package io.gitlab.jfronny.resclone.io; import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; public class MoveDirVisitor extends SimpleFileVisitor { private final Path fromPath; private final Path toPath; private final CopyOption copyOption; public MoveDirVisitor(Path fromPath, Path toPath, CopyOption copyOption) { this.fromPath = fromPath; this.toPath = toPath; this.copyOption = copyOption; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Path targetPath = toPath.resolve(fromPath.relativize(dir)); if(!Files.exists(targetPath)) Files.createDirectory(targetPath); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.copy(file, toPath.resolve(fromPath.relativize(file)), copyOption); Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }