Java Object Deep Copy deepCopy(File fromDir, File toDir)

Here you can find the source of deepCopy(File fromDir, File toDir)

Description

Copy the contents of fromDir into toDir (if the latter is missing it will be created)

License

Open Source License

Parameter

Parameter Description
fromDir a parameter
toDir a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void deepCopy(File fromDir, File toDir) throws IOException 

Method Source Code

//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2013 OpenPlans// w w  w. ja v  a  2s .c  om
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

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 the contents of fromDir into toDir (if the latter is missing it will
     * be created)
     * 
     * @param fromDir
     * @param toDir
     * @throws IOException
     */
    public static void deepCopy(File fromDir, File toDir) throws IOException {
        if (!fromDir.isDirectory() || !fromDir.exists())
            throw new IllegalArgumentException(
                    "Invalid source directory " + "(it's either not a directory, or does not exist");
        if (toDir.exists() && toDir.isFile())
            throw new IllegalArgumentException(
                    "Invalid destination directory, " + "it happens to be a file instead");

        // create destination if not available
        if (!toDir.exists())
            if (!toDir.mkdir())
                throw new IOException("Could not create " + toDir);

        File[] files = fromDir.listFiles();
        for (File file : files) {
            File destination = new File(toDir, file.getName());
            if (file.isDirectory())
                deepCopy(file, destination);
            else
                copy(file, destination);
        }
    }

    /**
     * Copies the provided input stream onto a file
     * 
     * @param from
     * @param to
     * @throws IOException
     */
    public static void copy(InputStream from, File to) throws IOException {
        copy(from, new FileOutputStream(to));
    }

    /**
     * Copies the provided input stream onto an outputstream
     * 
     * @param from
     * @param to
     * @throws IOException
     */
    public static void copy(InputStream from, OutputStream out) throws IOException {
        try {
            byte[] buffer = new byte[1024 * 16];
            int bytes = 0;
            while ((bytes = from.read(buffer)) != -1)
                out.write(buffer, 0, bytes);

            out.flush();
        } finally {
            if (from != null) {
                from.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * Copies the provided file onto the specified destination file
     * 
     * @param from
     * @param to
     * @throws IOException
     */
    public static void copy(File from, File to) throws IOException {
        copy(new FileInputStream(from), to);
    }
}

Related

  1. deepCopy(final Class c, final Serializable s)
  2. deepCopy(final Object oldObj)
  3. deepCopy(List src)
  4. deepCopy(List src)