Search code examples
javagrailsfile-iogroovy

Copy entire directory contents to another directory?


Method to copy entire directory contents to another directory in java or groovy?


Solution

  • FileUtils.copyDirectory()

    Copies a whole directory to a new location preserving the file dates. This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory.

    The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

    To do so, here's the example code

    String source = "C:/your/source";
    File srcDir = new File(source);
    
    String destination = "C:/your/destination";
    File destDir = new File(destination);
    
    try {
        FileUtils.copyDirectory(srcDir, destDir);
    } catch (IOException e) {
        e.printStackTrace();
    }