Java Path Copy nio copyRecursively(final Path source, final Path destination, final CopyOption... options)

Here you can find the source of copyRecursively(final Path source, final Path destination, final CopyOption... options)

Description

Recursively copies one path to another.

License

Open Source License

Parameter

Parameter Description
source The path that should be copied.
destination The directory to place copied files in.
options Options to use when copying.

Exception

Parameter Description
IOException If any files couldn't be copied.

Declaration

public static void copyRecursively(final Path source, final Path destination, final CopyOption... options)
        throws IOException 

Method Source Code


//package com.java2s;
/*/*from ww w. j a  v  a2  s.  c o  m*/
 * Copyright (c) 2006-2015 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.io.IOException;

import java.nio.file.CopyOption;
import java.nio.file.DirectoryStream;

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

public class Main {
    /**
     * Recursively copies one path to another. Once complete, a deep copy of the source file or
     * folder will be present in the destination directory.
     *
     * @param source The path that should be copied.
     * @param destination The directory to place copied files in.
     * @param options Options to use when copying.
     * @throws IOException If any files couldn't be copied.
     */
    public static void copyRecursively(final Path source, final Path destination, final CopyOption... options)
            throws IOException {
        doCopyRecursively(source, destination, false, options);
    }

    private static void doCopyRecursively(final Path source, final Path destination, final boolean contents,
            final CopyOption... options) throws IOException {
        final Path destinationPath;
        if (contents) {
            destinationPath = destination;
        } else {
            destinationPath = destination.resolve(source.getFileName().toString());
        }

        if (Files.isDirectory(source)) {
            Files.createDirectories(destinationPath);
            try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(source)) {
                for (Path path : directoryStream) {
                    copyRecursively(path, destinationPath, options);
                }
            }
        } else {
            Files.copy(source, destinationPath, options);
        }
    }
}

Related

  1. copyFileToDataFolder(ClassLoader cl, String resourceName, Path targetFolder, boolean replace)
  2. copyRecursive(Path source, Path target, CopyOption... options)
  3. copyRecursively(File fromDirectory, File toDirectory)
  4. copyRecursively(File src, File dest)
  5. copyRecursively(File src, File dest)
  6. copyRecursively(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
  7. copyRecursivelyHelper(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
  8. copyResource(String resourceLocation, Path target)
  9. copyResource(String source, Path target, Class cls)