Copy entire directory contents to another directory?

Sanal MS picture Sanal MS · Jun 2, 2011 · Viewed 149.8k times · Source

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

Answer

Jigar Joshi picture Jigar Joshi · Jun 2, 2011

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();
}