Java Path Copy nio copyTree(final Path source, final Path target)

Here you can find the source of copyTree(final Path source, final Path target)

Description

Recursively copy the contents of source into the target; target does not have to exist, it can exist.

License

Open Source License

Declaration

public static void copyTree(final Path source, final Path target) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.IOException;

import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;

import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    /**//from   ww w  . j  a va 2s  .  c om
     * Recursively copy the contents of source into the target; target does not have to exist, it can
     * exist. Does not follow the symbolic links.
     */
    public static void copyTree(final Path source, final Path target) throws IOException {
        if (!Files.exists(source)) {
            throw new IOException(String.format("Can not copy: source directory %s does not exist",
                    source.toAbsolutePath().toString()));
        }
        Files.createDirectories(target);
        Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
            Path currentTarget = target;

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                if (!source.equals(dir)) {
                    currentTarget = currentTarget.resolve(dir.getFileName().toString());
                    Files.createDirectories(currentTarget);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.copy(file, currentTarget.resolve(file.getFileName().toString()));
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
                currentTarget = currentTarget.getParent();
                return FileVisitResult.CONTINUE;
            }
        });
    }

    /**
     * Resolves the Path to the file or directory under the <code>
     * directory/parts[0]/parts[1]/.../parts[n]</code>.
     *
     * @param directory root directory for resolve
     * @param parts parts of the path relative to directory
     * @return resolved Path
     */
    public static Path resolve(Path directory, String... parts) {
        Path current = directory;
        for (String part : parts) {
            current = current.resolve(part);
        }
        return current;
    }
}

Related

  1. copyRecursivelyHelper(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
  2. copyResource(String resourceLocation, Path target)
  3. copyResource(String source, Path target, Class cls)
  4. copyResources(final URL source, final Path destination)
  5. copyToDir(Path source, Path targetDir, CopyOption... options)
  6. createDirectories(Path currentRestorePath)
  7. createDirectories(Path path, FileAttribute... attrs)
  8. createDirectories(Path thePath, FileAttribute... theAttributes)
  9. createDirectoriesForFile(Path file)