import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Test { public static void main(String[] args) { try { new Test().copyDir("demo", "test"); } catch (IOException e) { e.printStackTrace(); } } public void copyDir(String from, String to) throws IOException { File folder = new File(to); if(!folder.exists() && !folder.isDirectory()) folder.mkdir(); File f = new File(from); File[] it = f.listFiles(); for (int i = 0; i < it.length; i++) { String str1 = it[i].toString(); String str2 = str1.replace(from, to); if (new File(str1).isDirectory()) { new File(str2).mkdir(); copyDir(str1,str2); } else { InputStream input = null; OutputStream output = null; try { input = new FileInputStream(str1); output = new FileOutputStream(str2); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } } finally { input.close(); output.close(); } } } } }