Java Utililty Methods Path Copy nio

List of utility methods to do Path Copy nio

Description

The list of methods to do Path Copy nio are organized into topic(s).

Method

voidcopyFilesRecursively(final Path from, final Path to)
Copies files from from directory to to directory.
Preconditions.checkArgument(Files.isDirectory(from), "%s (from) is not a directory");
Preconditions.checkArgument(Files.isDirectory(to), "%s (to) is not a directory");
Files.walkFileTree(from, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Path destFile = to.resolve(from.relativize(file));
        Files.createDirectories(destFile.getParent());
        if (Files.isSymbolicLink(file)) {
...
voidcopyFileToArchive(InputStream srcInputStream, Path destPath)
Copy file to archive.
Path parent = destPath.getParent();
if (parent != null) {
    if (Files.notExists(parent)) {
        Files.createDirectories(parent);
    Files.copy(srcInputStream, destPath, StandardCopyOption.REPLACE_EXISTING);
PathcopyFileToDataFolder(ClassLoader cl, String resourceName, Path targetFolder, boolean replace)
Copies the given file from the jar to the data folder.
if (resourceName == null || "".equals(resourceName)) {
    throw new IllegalArgumentException("You need to provide a source file name.");
if (targetFolder == null) {
    throw new IllegalArgumentException("The destination is not defined.");
Path targetFile = targetFolder.resolve(resourceName);
createDirectoriesForFile(targetFile);
...
voidcopyRecursive(Path source, Path target, CopyOption... options)
copy Recursive
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        Files.createDirectories(target.resolve(source.relativize(dir)));
        return FileVisitResult.CONTINUE;
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
...
voidcopyRecursively(File fromDirectory, File toDirectory)
copy Recursively
copyRecursively(fromDirectory, toDirectory, null);
voidcopyRecursively(File src, File dest)
copy Recursively
copyRecursively(src.toPath(), dest.toPath());
voidcopyRecursively(File src, File dest)
copy Recursively
copyRecursively(src.toPath(), dest.toPath());
voidcopyRecursively(final Path source, final Path destination, final CopyOption... options)
Recursively copies one path to another.
doCopyRecursively(source, destination, false, options);
voidcopyRecursively(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
copy Recursively
try {
    if (!Files.exists(source, LinkOption.NOFOLLOW_LINKS)) {
        throw new RuntimeException("Source path does not exist " + source.toAbsolutePath().toString());
    if (!Files.isDirectory(source)) {
        copyRecursivelyHelper(source, target, bytesCounter, copyOptions);
    } else {
        if (Files.exists(target) && !Files.isDirectory(target)) {
...
voidcopyRecursivelyHelper(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
Assumes that 1) source exists 2) target parent exists
try {
    if (!Files.isDirectory(source)) {
        Files.copy(source, target, copyOptions);
        if (bytesCounter != null) {
            BasicFileAttributes attributes = Files.readAttributes(source, BasicFileAttributes.class);
            bytesCounter.addAndGet(attributes.size());
    } else {
...