Description
Implementation of recursive directory copy, does NOT overwrite.
License
Apache License
Parameter
Parameter | Description |
---|
source | an existing source directory to copy from |
destination | an existing destination directory to copy to |
excludes | a list of paths in "source" to exclude |
Exception
Parameter | Description |
---|
IllegalArgumentException | if source directory is same destination directory, eithersource or destination is not a directory, or destination is inside source |
Declaration
public static void copyDirectory(final Path source, final Path destination, List<Path> excludes)
throws IOException
Method Source Code
//package com.java2s;
/*/* w w w . jav a 2s .c o m*/
* Copyright 2016 Google LLC.
*
* 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.CopyOption;
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;
import java.util.Collections;
import java.util.List;
public class Main {
/**
* Implementation of recursive directory copy, does NOT overwrite.
*
* @param source an existing source directory to copy from
* @param destination an existing destination directory to copy to
* @throws IllegalArgumentException if source directory is same destination directory, either
* source or destination is not a directory, or destination is inside source
*/
public static void copyDirectory(final Path source, final Path destination) throws IOException {
copyDirectory(source, destination, Collections.emptyList());
}
/**
* Implementation of recursive directory copy, does NOT overwrite.
*
* @param source an existing source directory to copy from
* @param destination an existing destination directory to copy to
* @param excludes a list of paths in "source" to exclude
* @throws IllegalArgumentException if source directory is same destination directory, either
* source or destination is not a directory, or destination is inside source
*/
public static void copyDirectory(final Path source, final Path destination, List<Path> excludes)
throws IOException {
Preconditions.checkNotNull(source);
Preconditions.checkNotNull(destination);
Preconditions.checkArgument(Files.isDirectory(source), "Source is not a directory");
Preconditions.checkArgument(Files.isDirectory(destination), "Destination is not a directory");
Preconditions.checkArgument(!Files.isSameFile(source, destination), "Source and destination are the same");
Preconditions.checkArgument(!destination.toAbsolutePath().startsWith(source.toAbsolutePath()),
"destination is child of source");
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
final CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.COPY_ATTRIBUTES };
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (dir.equals(source)) {
return FileVisitResult.CONTINUE;
}
if (excludes.contains(dir)) {
return FileVisitResult.SKIP_SUBTREE;
}
Files.copy(dir, destination.resolve(source.relativize(dir)), copyOptions);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (excludes.contains(file)) {
return FileVisitResult.CONTINUE;
}
Files.copy(file, destination.resolve(source.relativize(file)), copyOptions);
return FileVisitResult.CONTINUE;
}
});
}
}
Related
- copyDirectory(File srcDir, File destDir, boolean preserveFileDate)
- copyDirectory(final File source, final File destination)
- copyDirectory(final File sourceFile, final File targetDir)
- copyDirectory(final File srcDir, final File destDir)
- copyDirectory(final Path source, final Path destination)
- copyDirectory(Path source, ArrayList targets)
- copyDirectory(String fromPath, String toPath)
- copyDirectory(String source, String destination)
- copyDirectoryRecursively(File source, File target)