Java Directory Copy nio copyDirectory(File source, File dest, CopyOption... options)

Here you can find the source of copyDirectory(File source, File dest, CopyOption... options)

Description

https://stackoverflow.com/questions/11651900/how-to-recursively-copy-entire-directory-including-parent-folder-in-java

License

Apache License

Parameter

Parameter Description
source - directory to copy from
dest - directory to copy to
options - copy mode

Exception

Parameter Description
IOException - thrown by underlying nio calls

Declaration

private static void copyDirectory(File source, File dest, CopyOption... options) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;

import java.nio.file.*;

public class Main {
    /**// ww  w . ja v a2 s.c o m
     * https://stackoverflow.com/questions/11651900/how-to-recursively-copy-entire-directory-including-parent-folder-in-java
     *
     * @param source - directory to copy from
     * @param dest - directory to copy to
     * @param options - copy mode
     * @throws IOException - thrown by underlying nio calls
     */
    private static void copyDirectory(File source, File dest, CopyOption... options) throws IOException {
        if (!dest.exists())
            dest.mkdirs();
        File[] contents = source.listFiles();
        if (contents != null) {
            for (File f : contents) {
                File newFile = new File(dest.getAbsolutePath() + File.separator + f.getName());
                if (f.isDirectory())
                    copyDirectory(f, newFile, options);
                else
                    copyFile(f, newFile, options);
            }
        }
    }

    private static void copyFile(File source, File dest, CopyOption... options) throws IOException {
        Files.copy(source.toPath(), dest.toPath(), options);
    }
}

Related

  1. copyDir(String sourceDir, String targetDir)
  2. copyDir(String src, String dst)
  3. copyDirectiory(String sourceDir, String targetDir)
  4. copyDirectory(File in, File out)
  5. copyDirectory(File in, File out)
  6. copyDirectory(File source, File destination)
  7. copyDirectory(File source, File destination)
  8. copyDirectory(File source, File destination)
  9. copyDirectory(File source, File destination, boolean recursive)