Java Path Copy nio copyFilesRecursively(final Path from, final Path to)

Here you can find the source of copyFilesRecursively(final Path from, final Path to)

Description

Copies files from from directory to to directory.

License

Apache License

Declaration

public static void copyFilesRecursively(final Path from, final Path to) throws IOException 

Method Source Code

//package com.java2s;
/*//w w w  . j a v  a  2s.com
 * Copyright (C) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.common.base.Preconditions;

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.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    /**
     * Copies files from {@code from} directory to {@code to} directory. If any file exist in the
     * destination it fails instead of overwriting.
     *
     * <p>File attributes are also copied.
     *
     * <p>Symlinks are kept as in the origin. If a symlink points to "../foo" it will point to
     * that "../foo" in the destination. If it points to "/usr/bin/foo" it will point to
     * "/usr/bin/foo"
     */
    public static void copyFilesRecursively(final Path from, final Path to) throws IOException {
        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)) {
                    Path realDestination = Files.readSymbolicLink(file);
                    Files.createSymbolicLink(destFile, realDestination);
                    return FileVisitResult.CONTINUE;
                }
                Files.copy(file, destFile, StandardCopyOption.COPY_ATTRIBUTES);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

Related

  1. copyFileFromResourcesToServer(String resourceFile, Path targetDirectory, boolean override)
  2. copyFiles()
  3. copyFiles(String srcPath, String destPath)
  4. copyFiles(String urlDir, String outPath)
  5. copyFilesAndApplyPermissions(Path sourceDir, Path targetDir, List filenames)
  6. copyFileToArchive(InputStream srcInputStream, Path destPath)
  7. copyFileToDataFolder(ClassLoader cl, String resourceName, Path targetFolder, boolean replace)
  8. copyRecursive(Path source, Path target, CopyOption... options)
  9. copyRecursively(File fromDirectory, File toDirectory)