Không có phương thức copy thư mục nào sẵn có trong java. Để copy một thư mục trong java từ vị trí này sang vị trí khác với tất cả các thư mục con và các file mà chúng chứa thì chúng ta phải copy từng thư mục con và file sang vị trí mới.
Nội dung chính
Ví dụ về copy thư mục trong java
Ví dụ 1: sử dụng java.nio.file.Files.copy()
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class CopyFolderExample2 {
    /**
     * main
     * 
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // sourceFolder: thu muc nguon duoc copy
        File sourceFolder = new File("D:\\temp");
        // targetFolder: thu muc dich duoc copy den
        File targetFolder = new File("D:\\tempNew");
        // goi phuong thuc copy
        copyFolder(sourceFolder, targetFolder);
    }
    /**
     * copy folder
     * 
     * @param sourceFolder
     * @param targetFolder
     * @throws IOException
     */
    private static void copyFolder(File sourceFolder, File targetFolder) 
            throws IOException {
        // Check neu sourceFolder la mot thu muc hoac file
        // neu sourceFolder la file thi copy den thu muc dich
        if (sourceFolder.isDirectory()) {
            // Xac nhan neu targetFolder chua ton tai thi tao moi 
            if (!targetFolder.exists()) {
                targetFolder.mkdir();
                System.out.println("Thu muc da duoc tao " + targetFolder);
            }
            // Liet ke tat ca cac file va thu muc trong sourceFolder
            String files[] = sourceFolder.list();
            for (String file : files) {
                File srcFile = new File(sourceFolder, file);
                File tarFile = new File(targetFolder, file);
                // goi lai phuong thuc copyFolder
                copyFolder(srcFile, tarFile);
            }
        } else {
            // copy file tu thuc muc nguon den thu muc dich
            Files.copy(sourceFolder.toPath(), targetFolder.toPath(), 
                    StandardCopyOption.REPLACE_EXISTING);
            System.out.println("File da duoc copy " + targetFolder);
        }
    }
}
Output:
Thu muc da duoc tao D:\tempNew Thu muc da duoc tao D:\tempNew\java Thu muc da duoc tao D:\tempNew\java\java-core File da duoc copy D:\tempNew\java\java-core\javacore1.txt Thu muc da duoc tao D:\tempNew\src File da duoc copy D:\tempNew\src\test1.txt File da duoc copy D:\tempNew\test2.txt File da duoc copy D:\tempNew\test3.txt
Ví dụ 2: sủ dụng FileInputStream và FileOutputStream
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFolderExample1 {
    /**
     * main
     * 
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // sourceFolder: thu muc nguon duoc copy
        File sourceFolder = new File("D:\\temp");
        // targetFolder: thu muc dich duoc copy den
        File targetFolder = new File("D:\\tempNew");
        // goi phuong thuc copy
        copyFolder(sourceFolder, targetFolder);
    }
    /**
     * copy folder
     * 
     * @param sourceFolder
     * @param targetFolder
     * @throws IOException
     */
    private static void copyFolder(File sourceFolder, File targetFolder) 
            throws IOException {
        // Check neu sourceFolder la mot thu muc hoac file
        // neu sourceFolder la file thi copy den thu muc dich
        if (sourceFolder.isDirectory()) {
            // Xac nhan neu targetFolder chua ton tai thi tao moi
            if (!targetFolder.exists()) {
                targetFolder.mkdir();
                System.out.println("Thu muc da duoc tao " + targetFolder);
            }
            // Liet ke tat ca cac file va thu muc trong sourceFolder
            String files[] = sourceFolder.list();
            for (String file : files) {
                File srcFile = new File(sourceFolder, file);
                File tarFile = new File(targetFolder, file);
                // goi lai phuong thuc copyFolder
                copyFolder(srcFile, tarFile);
            }
        } else {
            // copy file tu thuc muc nguon den thu muc dich
            InputStream in = new FileInputStream(sourceFolder);
            OutputStream out = new FileOutputStream(targetFolder);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            System.out.println("File da duoc copy " + targetFolder);
            in.close();
            out.close();
        }
    }
}
Output:
Thu muc da duoc tao D:\tempNew Thu muc da duoc tao D:\tempNew\java Thu muc da duoc tao D:\tempNew\java\java-core File da duoc copy D:\tempNew\java\java-core\javacore1.txt Thu muc da duoc tao D:\tempNew\src File da duoc copy D:\tempNew\src\test1.txt File da duoc copy D:\tempNew\test2.txt File da duoc copy D:\tempNew\test3.txt
 
                      