Java Copy Directory copyDirectory(File src, File dst)

Here you can find the source of copyDirectory(File src, File dst)

Description

Copy a directory and its contents.

License

Open Source License

Parameter

Parameter Description
src The name of the directory to copy.
dst The name of the destination directory.

Exception

Parameter Description
IOException If the directory could not be copied.

Declaration

public static void copyDirectory(File src, File dst) throws IOException 

Method Source Code


//package com.java2s;
/*/*w w w .  j  a va 2s  .com*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2011-2015 ForgeRock AS
 *      Portions Copyright 2013 Manuel Gaupp
 */

import java.io.*;

public class Main {
    /**
     * Copy a directory and its contents.
     *
     * @param src
     *          The name of the directory to copy.
     * @param dst
     *          The name of the destination directory.
     * @throws IOException
     *           If the directory could not be copied.
     */
    public static void copyDirectory(File src, File dst) throws IOException {
        if (src.isDirectory()) {
            // Create the destination directory if it does not exist.
            if (!dst.exists()) {
                dst.mkdirs();
            }

            // Recursively copy sub-directories and files.
            for (String child : src.list()) {
                copyDirectory(new File(src, child), new File(dst, child));
            }
        } else {
            copyFile(src, dst);
        }
    }

    /**
     * Copy a file.
     *
     * @param src
     *          The name of the source file.
     * @param dst
     *          The name of the destination file.
     * @throws IOException
     *           If the file could not be copied.
     */
    public static void copyFile(File src, File dst) throws IOException {
        copyOrAppend(src, dst, false);
    }

    private static void copyOrAppend(File src, File dst, boolean append) throws IOException {
        try (InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst, append)) {
            // Transfer bytes from in to out
            byte[] buf = new byte[8192];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
    }
}

Related

  1. copyDirectory(File src, File dest)
  2. copyDirectory(File src, File dest, boolean force)
  3. copyDirectory(File srcDir, File destDir)
  4. copyDirectory(File srcDir, File dstDir)
  5. copyDirectory(File srcDir, File dstDir)
  6. copyDirectory(File srcDir, File dstDir)