Close

Java IO & NIO - Copy a directory and its subdirectories/files into another directory

Java IO & NIO Java 

This example demonstrate how to copy entire directory contents to another directory. The top level destination directory can have a different name. It's based on Java 1.7 Java IO visitor pattern to recursively visit all files in a file tree.

Creating copy util class

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.EnumSet;
import java.util.Objects;

public class CopyUtil {
public static void copyDirectoryContent (File sourceFolder,
File destinationFolder) throws IOException {
if (sourceFolder.isDirectory()) {

if (destinationFolder.exists() && destinationFolder.isFile()) {
throw new IllegalArgumentException(
"Destination exists but is not a folder: "
+ destinationFolder
.getAbsolutePath());
}

if (!destinationFolder.exists()) {
Files.createDirectory(destinationFolder.toPath());
}

for (File file : sourceFolder.listFiles()) {
if (file.isDirectory()) {
copyDirectory(file, destinationFolder);
} else {
copyFile(file, destinationFolder);
}
}
}
}

public static void copyDirectory (File fromFile, File toParentFile)
throws IOException {
Path from = fromFile.toPath();
Path to = Paths.get(toParentFile.getAbsolutePath() + File.separatorChar + fromFile
.getName());

Files.walkFileTree(from, EnumSet.of(FileVisitOption.FOLLOW_LINKS),
Integer.MAX_VALUE, new MyCopyDirVisitor(from, to));
}

public static void copyFile (File toCopy, File mainDestination)
throws IOException {
if (!mainDestination.exists()) {
mainDestination.mkdirs();
}
Path to = Paths.get(mainDestination.getAbsolutePath() +
File.separatorChar + toCopy.getName());

Files.copy(toCopy.toPath(), to, StandardCopyOption.REPLACE_EXISTING);
}
}


Creating a custom FileVisitor implementation

The visitor pattern introduced in Java 1.7 is based on FileVisitor interface. Instead of implementing this interface, this example extends SimpleFileVisitor which has default behavior to visit all files.

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class MyCopyDirVisitor extends SimpleFileVisitor<Path> {

private Path fromPath;
private Path toPath;
private StandardCopyOption copyOption;


public MyCopyDirVisitor (Path fromPath, Path toPath, StandardCopyOption copyOption) {
this.fromPath = fromPath;
this.toPath = toPath;
this.copyOption = copyOption;
}

public MyCopyDirVisitor (Path fromPath, Path toPath) {
this(fromPath, toPath, StandardCopyOption.REPLACE_EXISTING);
}

@Override
public FileVisitResult preVisitDirectory (Path dir, BasicFileAttributes attrs)
throws IOException {

Path targetPath = toPath.resolve(fromPath.relativize(dir));
if (!Files.exists(targetPath)) {
Files.createDirectory(targetPath);
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile (Path file, BasicFileAttributes attrs)
throws IOException {

Files.copy(file, toPath.resolve(fromPath.relativize(file)), copyOption);
return FileVisitResult.CONTINUE;
}
}




Creating main class for a test.

public class CopyUtilTest {
public static void main (String[] args) throws IOException {
CopyUtil.copyDirectoryContent(new File("d:\\temp"), new File("d:\\temp-copy"));
}
}




See Also