Created
May 29, 2018 04:01
-
-
Save Pr0methean/3e4ecaa60c421f95875f892092ee0e55 to your computer and use it in GitHub Desktop.
Reinvented Files.createDirectories() wheel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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