Here you can find the source of copyDirectory(File sourceDir, File targetDir)
public static void copyDirectory(File sourceDir, File targetDir) throws IOException
//package com.java2s; import java.io.*; public class Main { private final static int BUFFER = 8192; public static void copyDirectory(File sourceDir, File targetDir) throws IOException { //??????/* w ww .j a va 2 s . com*/ targetDir.mkdirs(); if (null != sourceDir) { //???????????????? File[] file = sourceDir.listFiles(); for (int i = 0; i < file.length; i++) { if (file[i].isFile()) { File sourceFile = file[i]; File targetFile = new File(targetDir.getAbsolutePath() + File.separator + file[i].getName()); copyFile(sourceFile, targetFile); } else if (file[i].isDirectory()) { File dir1 = new File(sourceDir, file[i].getName()); File dir2 = new File(targetDir, file[i].getName()); copyDirectory(dir1, dir2); } } } } public static void copyFile(File sourceFile, File targetFile) throws IOException { BufferedInputStream inBuff = null; BufferedOutputStream outBuff = null; if (null != sourceFile && null != targetFile) { try { inBuff = new BufferedInputStream(new FileInputStream( sourceFile)); outBuff = new BufferedOutputStream(new FileOutputStream( targetFile)); byte[] buffer = new byte[BUFFER]; int length; while ((length = inBuff.read(buffer)) != -1) { outBuff.write(buffer, 0, length); } outBuff.flush(); } finally { if (inBuff != null) { inBuff.close(); } if (outBuff != null) { outBuff.close(); } } } } }