Here you can find the source of copyDirectory(File srcPath, File dstPath)
Parameter | Description |
---|---|
srcPath | source path |
dstPath | destination path |
Parameter | Description |
---|---|
IOException | if any exception occurred |
public static void copyDirectory(File srcPath, File dstPath) throws IOException
//package com.java2s; /*/*from w w w .j a va 2 s . c o m*/ * Copyright (C) 2009 eXo Platform SAS. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ 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 { /** * Copy directory. * * @param srcPath * source path * @param dstPath * destination path * @throws IOException * if any exception occurred */ public static void copyDirectory(File srcPath, File dstPath) throws IOException { if (srcPath.isDirectory()) { if (!dstPath.exists()) { dstPath.mkdirs(); } String files[] = srcPath.list(); for (int i = 0; i < files.length; i++) { copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i])); } } else { InputStream in = null; OutputStream out = null; try { in = new FileInputStream(srcPath); out = new FileOutputStream(dstPath); transfer(in, out); } finally { if (in != null) { in.close(); } if (out != null) { out.flush(); out.close(); } } } } /** * Transfer bytes from in to out */ public static void transfer(InputStream in, OutputStream out) throws IOException { byte[] buf = new byte[2048]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } }