Java Directory Copy copyDirectory(File sourceDir, File destDir)

Here you can find the source of copyDirectory(File sourceDir, File destDir)

Description

Copy recursively one directory to another.

License

Open Source License

Parameter

Parameter Description
sourceDir a parameter
destDir a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyDirectory(File sourceDir, File destDir) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2008(c) The OBiBa Consortium. All rights reserved.
 * //w w  w . j  av  a  2s  .  c o  m
 * This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

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 recursively one directory to another.
     * @param sourceDir
     * @param destDir
     * @throws IOException
     */
    public static void copyDirectory(File sourceDir, File destDir) throws IOException {
        if (sourceDir == null)
            throw new IllegalArgumentException("sourceDir cannot be null");
        if (destDir == null)
            throw new IllegalArgumentException("destDir cannot be null");

        if (!destDir.exists()) {
            if (destDir.mkdir() == false) {
                throw new IOException("Cannot create destination directory " + destDir.getAbsolutePath());
            }
        }

        if (sourceDir.exists()) {
            File[] children = sourceDir.listFiles();
            if (children != null) {
                for (File sourceChild : children) {
                    String name = sourceChild.getName();
                    File destChild = new File(destDir, name);
                    if (sourceChild.isDirectory()) {
                        copyDirectory(sourceChild, destChild);
                    } else {
                        copyFile(sourceChild, destChild);
                    }
                }
            }
        }
    }

    /**
     * Copy a normal file to another.
     * @param source
     * @param dest destination directory or destination file
     * @throws IOException
     */
    public static void copyFile(File source, File dest) throws IOException {
        if (source == null)
            throw new IllegalArgumentException("source cannot be null");
        if (dest == null)
            throw new IllegalArgumentException("dest cannot be null");

        if (dest.isDirectory()) {
            dest = new File(dest, source.getName());
        }

        if (!dest.exists()) {
            if (dest.createNewFile() == false) {
                throw new IOException("Cannot create destination file " + dest.getAbsolutePath());
            }
        }

        InputStream in = null;
        OutputStream out = null;

        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(dest);

            byte[] buf = new byte[4096];
            int len;

            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                // ignore
            }
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

Related

  1. copyDirectory(File source, File target)
  2. copyDirectory(File source, File target)
  3. copyDirectory(File source, File target)
  4. copyDirectory(File source, File target)
  5. copyDirectory(File source, File target)
  6. copyDirectory(File sourceDir, File destDir)
  7. copyDirectory(File sourceDir, File destinationDir)
  8. copyDirectory(File sourceLocation, File targetLocation)
  9. copyDirectory(File sourceLocation, File targetLocation)