I see that Java 8 has significantly cleaned up reading the contents of a file into a String:
String contents = new String(Files.readAllBytes(Paths.get(new URI(so
Using Files.walkFileTree
:
Files.walk
)IOException
elegantly.printStackTrace
) public void copyFolder(Path source, Path target, CopyOption... options)
throws IOException {
Files.walkFileTree(source, new SimpleFileVisitor() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
createDirectories(target.resolve(source.relativize(dir)));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
copy(file, target.resolve(source.relativize(file)), options);
return FileVisitResult.CONTINUE;
}
});
}
What this does is
preVisitDirectory
):visitFile
):options can be used to tailor the copy to your needs. For example to overwrite existing files in the target directory, use copyFolder(source, target, StandardCopyOption.REPLACE_EXISTING);