Java Path Copy nio copy(Path source, Path target, CopyOption... options)

Here you can find the source of copy(Path source, Path target, CopyOption... options)

Description

copy

License

Open Source License

Parameter

Parameter Description
source The file or directory to copy
target The path to copy the file or directory to
options The options to perform the operation with

Exception

Parameter Description
IOException if copying failed

Declaration

public static void copy(Path source, Path target, CopyOption... options) throws IOException 

Method Source Code

//package com.java2s;
/**/*from www .  ja  v a2 s .co m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 */

import java.io.IOException;

import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;

public class Main {
    /**
     * @param source  The file or directory to copy
     * @param target  The path to copy the file or directory to
     * @param options The options to perform the operation with
     * @throws IOException if copying failed
     */
    public static void copy(Path source, Path target, CopyOption... options) throws IOException {
        Files.createDirectories(target.getParent());
        for (Path currentSource : collect(source)) {
            Path currentTarget = target.resolve(source.relativize(currentSource));
            Files.copy(currentSource, currentTarget, options);
        }
    }

    /**
     * @param path The file or directory to collect paths from
     * @param pathMatchers The matchers to filter paths with
     * @return All matched paths within the given path or the path itself if it is a file and has been matched
     * @throws IOException if path is a directory and an error occurs visiting it
     */
    public static List<Path> collect(Path path, final PathMatcher... pathMatchers) throws IOException {
        final List<Path> paths = new LinkedList<>();
        if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    return collectPath(dir);
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    return collectPath(file);
                }

                private FileVisitResult collectPath(Path path) {
                    if (matches(path, pathMatchers)) {
                        paths.add(path);
                    }
                    return FileVisitResult.CONTINUE;
                }

            });
        }
        if (!paths.contains(path) && matches(path, pathMatchers)) {
            paths.add(0, path);
        }
        return paths;
    }

    /**
     * @param target The target to resolve paths against
     * @param paths  The paths to resolve
     * @return The resolved paths
     */
    public static List<Path> resolve(Path target, Collection<Path> paths) {
        List<Path> resolvedPaths = new ArrayList<>();
        for (Path child : paths) {
            resolvedPaths.add(target.resolve(child));
        }
        return resolvedPaths;
    }

    /**
     * @param target The target to relativize paths against
     * @param paths  The paths to relativize
     * @return The relativized paths
     */
    public static List<Path> relativize(Path target, Collection<Path> paths) {
        List<Path> relativizedPaths = new ArrayList<>();
        for (Path path : paths) {
            relativizedPaths.add(target.relativize(path));
        }
        return relativizedPaths;
    }

    private static boolean matches(Path path, PathMatcher... pathMatchers) {
        for (PathMatcher pathMatcher : pathMatchers) {
            if (!pathMatcher.matches(path)) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. copy(Path from, Path to)
  2. copy(Path inPath, Path outPath)
  3. copy(Path source, Path destination)
  4. copy(Path source, Path destination)
  5. copy(Path source, Path target)
  6. copy(Path sourcePath, Path destinationPath)
  7. copy(Path sourcePath, Path targetPath)
  8. copy(Path src, Path dest, List globPattern)
  9. copy(String filePath, String fileName)