Here you can find the source of copyDirectoryContents(File srcDir, File dstDir)
public static void copyDirectoryContents(File srcDir, File dstDir) throws IOException
//package com.java2s; /*//w w w .j a v a 2 s . c om * Copyright 2005,2006 WSO2, Inc. http://www.wso2.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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 Main { public static void copyDirectoryContents(File srcDir, File dstDir) throws IOException { File[] listFiles = srcDir.listFiles(); if (listFiles != null) { for (File srcFile : listFiles) { if (srcFile.isDirectory()) { copyDirectory(srcFile, new File(dstDir, srcFile.getName())); } else { copy(srcFile, new File(dstDir, srcFile.getName())); } } } } public static void copyDirectory(File srcDir, File dstDir) throws IOException { if (srcDir.isDirectory()) { if (!dstDir.exists()) { dstDir.mkdirs(); } String[] children = srcDir.list(); for (int i = 0; i < children.length; i++) { copyDirectory(new File(srcDir, children[i]), new File(dstDir, children[i])); } } else { copy(srcDir, dstDir); } } public static void copy(File src, File dst) throws IOException { if (dst.getParentFile() != null && !dst.getParentFile().exists()) { dst.getParentFile().mkdirs(); } InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } }