Java Copy Directory copyDirectory(File srcPath, File dstPath)

Here you can find the source of copyDirectory(File srcPath, File dstPath)

Description

Copy directory.

License

Open Source License

Parameter

Parameter Description
srcPath source path
dstPath destination path

Exception

Parameter Description
IOException if any exception occurred

Declaration

public static void copyDirectory(File srcPath, File dstPath) throws IOException 

Method Source Code

//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);
        }
    }
}

Related

  1. copyDirectory(File srcDir, File dstDir)
  2. copyDirectory(File srcDir, File dstDir)
  3. copyDirectory(File srcDir, File dstDir)
  4. copyDirectory(File srcDir, File dstDir)
  5. copyDirectory(File srcFile, File destFile)
  6. copyDirectory(File srcPath, File dstPath)
  7. copyDirectory(final File sourceDir, final File destDir)
  8. copyDirectory(final File src, final File dest)
  9. copyDirectory(final String sourceDir, final String targetDir)