Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pr0methean/3e4ecaa60c421f95875f892092ee0e55 to your computer and use it in GitHub Desktop.
Save Pr0methean/3e4ecaa60c421f95875f892092ee0e55 to your computer and use it in GitHub Desktop.
Reinvented Files.createDirectories() wheel
Files.walkFileTree(srcPath, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
Path target = destPath.resolve(srcPath.relativize(dir));
if (!Files.exists(target)) {
Files.createDirectory(target);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
Files.copy(file, destPath.resolve(srcPath
.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file,
IOException exc) throws IOException {
logger.warning(
"Importing file " + srcPath.relativize(file) + " + failed: " + exc);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir,
IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment