Java Utililty Methods Directory Copy nio

List of utility methods to do Directory Copy nio

Description

The list of methods to do Directory Copy nio are organized into topic(s).

Method

voidcopyDirectory(File src, File dest)
Copies a directory recursively.
dest.mkdir();
for (File file : src.listFiles()) {
    if (file.isDirectory()) {
        copyDirectory(file, new File(dest, file.getName()));
    } else {
        copyFile(file, new File(dest, file.getName()));
voidcopyDirectory(File src, File target)
copy Directory
if (src.isDirectory() && target.isDirectory()) {
    File[] files = src.listFiles();
    for (int i = 0; i < files.length; i++) {
        File targetFile = new File(target, files[i].getName());
        copyFile(files[i], targetFile);
voidcopyDirectory(File srcDir, File destDir)
copy Directory
copyDirectory(srcDir, destDir, null);
voidcopyDirectory(File srcDir, File destDir, boolean preserveFileDate)
Copies a whole directory to a new location.
if (srcDir == null) {
    throw new NullPointerException("Source must not be null");
if (destDir == null) {
    throw new NullPointerException("Destination must not be null");
if (srcDir.exists() == false) {
    throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
...
voidcopyDirectory(final File source, final File destination)
copy Directory
if (!destination.exists())
    destination.mkdirs();
for (File f : source.listFiles()) {
    final File target = new File(destination.getAbsolutePath() + "/" + f.getName());
    if (f.isFile())
        copyFile(f, target);
    else
        copyDirectory(f, target);
...
voidcopyDirectory(final File sourceFile, final File targetDir)
copy Directory
if (!sourceFile.isDirectory()) {
    throw new IllegalStateException("Source file should be a directory.");
if (!targetDir.getParentFile().exists()) {
    if (!targetDir.getParentFile().mkdirs()) {
        throw new IOException("Could not create " + targetDir.getParentFile().getAbsolutePath());
if (Files.isSymbolicLink(sourceFile.toPath())) {
    Path linkTarget = Files.readSymbolicLink(sourceFile.toPath());
    Files.createSymbolicLink(targetDir.toPath(), linkTarget);
    return;
if (!targetDir.exists()) {
    if (!targetDir.mkdir()) {
        throw new IOException("Could not create " + targetDir.getAbsolutePath());
for (String file : sourceFile.list()) {
    if (file.startsWith(".attach_pid")) {
        continue;
    File sf = new File(sourceFile, file);
    File newFile = new File(targetDir, file);
    if (sf.isDirectory()) {
        copyDirectory(sf, newFile);
    } else {
        Files.copy(sf.toPath(), newFile.toPath(), LinkOption.NOFOLLOW_LINKS);
voidcopyDirectory(final File srcDir, final File destDir)
copy Directory
if (srcDir == null || !srcDir.exists())
    return;
if (destDir == null || !destDir.exists())
    destDir.mkdirs();
final File[] srcFiles = srcDir.listFiles();
if (srcFiles == null) { 
    throw new IOException("Failed to list contents of " + srcDir);
if (destDir.exists()) {
    if (destDir.isDirectory() == false) {
        throw new IOException("Destination '" + destDir + "' exists but is not a directory");
} else {
    if (!destDir.mkdirs() && !destDir.isDirectory()) {
        throw new IOException("Destination '" + destDir + "' directory cannot be created");
if (destDir.canWrite() == false) {
    throw new IOException("Destination '" + destDir + "' cannot be written to");
for (final File srcFile : srcFiles) {
    final File dstFile = new File(destDir, srcFile.getName());
    if (srcFile.isDirectory()) {
        copyDirectory(srcFile, dstFile);
    } else {
        copyFile(srcFile, dstFile);
voidcopyDirectory(final Path source, final Path destination)
Implementation of recursive directory copy, does NOT overwrite
Preconditions.checkNotNull(source);
Preconditions.checkNotNull(destination);
Preconditions.checkArgument(Files.isDirectory(source));
Preconditions.checkArgument(Files.isDirectory(destination));
Preconditions.checkArgument(!source.equals(destination));
Preconditions.checkArgument(!destination.startsWith(source), "destination is child of source");
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
    final CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.COPY_ATTRIBUTES };
...
voidcopyDirectory(final Path source, final Path destination, List excludes)
Implementation of recursive directory copy, does NOT overwrite.
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>() {
...
voidcopyDirectory(Path source, ArrayList targets)
copy Directory
for (Path target : targets) {
    Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
            new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    Path targetdir = target.resolve(source.relativize(dir));
                    try {
...