Here you can find the source of copyDirectory(File source, File destination, String endWith)
public static void copyDirectory(File source, File destination, String endWith) throws IOException
//package com.java2s; //License from project: Apache License import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void copyDirectory(File source, File destination, String endWith) throws IOException { if (source.exists() && source.isDirectory()) { if (!destination.exists()) { destination.mkdirs();/*from w ww .j av a 2s. c o m*/ } File[] fileArray = source.listFiles(); for (int i = 0; i < fileArray.length; i++) { if (fileArray[i].isDirectory()) { copyDirectory(fileArray[i], new File(destination.getPath() + File.separator + fileArray[i].getName()), endWith); } else { if (endWith != null) { if (!fileArray[i].getPath().toLowerCase().endsWith(endWith.toLowerCase())) { continue; } } copyFile(fileArray[i], new File(destination.getPath() + File.separator + fileArray[i].getName())); } } } } public static void copyDirectory(File source, File destination) throws IOException { copyDirectory(source, destination, null); } public static void copyFile(File source, File destination) throws IOException { if ((destination.getParentFile() != null) && (!destination.getParentFile().exists())) { destination.getParentFile().mkdirs(); } FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(source); fos = new FileOutputStream(destination); byte[] buffer = new byte[1024 * 4]; int n = 0; while ((n = fis.read(buffer)) != -1) { fos.write(buffer, 0, n); } } finally { closeStream(fis); closeStream(fos); } } public static void closeStream(Closeable stream) { if (stream != null) { try { stream.close(); } catch (IOException e) { System.out.println("Failed to close stream !"); } } } }