How do I copy directory with all its contents to another directory?

To copy a directory with the entire child directories and files we can use a handy method provided by the commons-io FileUtils.copyDirectory(). This method accept two parameters, the source directory and the destination directory. The source directory should be exist while if the destination directory doesn’t exists it will be created.

package org.kodejava.example.commons.io;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class DirectoryCopy {
    public static void main(String[] args) {
        //
        // An existing directory to copy.
        //
        String source = "C:/Demo/source";
        File srcDir = new File(source);

        //
        // The destination directory to copy to. This directory
        // doesn't exists and will be created during the copy
        // directory process.
        //
        String destination = "C:/Demo/target";
        File destDir = new File(destination);

        try {
            //
            // Copy source directory into destination directory
            // including its child directories and files. When
            // the destination directory is not exists it will
            // be created. This copy process also preserve the
            // date information of the file.
            //
            FileUtils.copyDirectory(srcDir, destDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply