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(String fromPath, String toPath)
Copy given directory, sub-directories and files
Path from = Paths.get(fromPath);
Path to = Paths.get(toPath);
CopyOption[] options = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING,
        StandardCopyOption.COPY_ATTRIBUTES };
Files.copy(from, to, options);
for (File item : from.toFile().listFiles()) {
    if (item.isDirectory()) {
        copyDirectory(item.toString(), toPath + File.separator + item.getName());
...
voidcopyDirectory(String source, String destination)
copy Directory
File sourceDir = new File(source);
File destDir = new File(destination);
if (!sourceDir.exists() || !sourceDir.isDirectory()) {
    throw new IOException("Source directory " + source + " does not exist or is not a directory");
if (destDir.exists() && !destDir.isDirectory()) {
    throw new IOException("Destination directory " + destination + " does not exist or is not a directory");
if (!destDir.exists()) {
    if (!destDir.mkdirs()) {
        throw new IOException("Directory " + destination + " cannot be created");
File[] files = sourceDir.listFiles();
for (File file : files) {
    if (file.isFile()) {
        copyFile(file.getPath(), destination + File.separator + file.getName());
    } else if (!file.getAbsolutePath().equals(destDir.getAbsolutePath())) {
        copyDirectory(file.getPath(), destination + File.separator + file.getName());
voidcopyDirectoryRecursively(File source, File target)
copy Directory Recursively
if (!target.exists()) {
    Files.copy(source.toPath(), target.toPath(), java.nio.file.StandardCopyOption.COPY_ATTRIBUTES);
for (String child : source.list()) {
    copyDirectory(new File(source, child), new File(target, child));
voidcopyDirectoryToDirectory(File srcDir, File destDir)
copy Directory To Directory
if (!srcDir.exists() || !srcDir.isDirectory()) {
    throw new IOException("Source dir " + srcDir + " is not a directory.");
if (destDir.exists() && !destDir.isDirectory()) {
    throw new IOException("Source dir " + destDir + " is not a directory.");
if (!destDir.exists() && !destDir.mkdirs()) {
    throw new IOException("Can't create " + destDir);
...
voidcopyDirNIO(File sourceLocation, File targetLocation)
Copy directory.
if (sourceLocation.isDirectory()) {
    if (!targetLocation.exists()) {
        targetLocation.mkdir();
    String[] children = sourceLocation.list();
    for (int i = 0; i < children.length; i++) {
        copyDirNIO(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
} else {
    copyUsingNIO(sourceLocation, targetLocation);