Java Directory Copy copyDirectory(File inDir, File outDir)

Here you can find the source of copyDirectory(File inDir, File outDir)

Description

Copy a complete directory tree from one directory to another.

License

Open Source License

Parameter

Parameter Description
inDir the directory to copy.
outDir the copy.

Return

true if the operation succeeded completely; false otherwise.

Declaration

public static synchronized boolean copyDirectory(File inDir, File outDir) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

import java.util.jar.JarFile;

import java.util.zip.ZipFile;

public class Main {
    /**/* ww w.  j  a va  2s .  co  m*/
     * Copy a complete directory tree from one directory to another.
     * @param inDir the directory to copy.
     * @param outDir the copy.
     * @return true if the operation succeeded completely; false otherwise.
     */
    public static synchronized boolean copyDirectory(File inDir, File outDir) {
        outDir.mkdirs();
        File[] files = inDir.listFiles();
        for (File inFile : files) {
            File outFile = new File(outDir, inFile.getName());
            if (inFile.isFile()) {
                if (!copy(inFile, outFile))
                    return false;
            } else {
                if (!copyDirectory(inFile, outFile))
                    return false;
            }
        }
        return true;
    }

    /**
     * Copy a file.
     * @param inFile the file to copy.
     * @param outFile the copy.
     * @return true if the operation succeeded completely; false otherwise.
     */
    public static synchronized boolean copy(File inFile, File outFile) {
        try {
            return copy(new FileInputStream(inFile), new FileOutputStream(outFile), -1);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * Copy a maximum number of bytes from an InputStream to an OutputStream.
     * The OutputStream is always closed when the operation is complete.
     * If the contentLength is positive, the InputStream is left open; else it is closed
     * when the operation is complete.
     * @param in the stream to copy.
     * @param out the copy.
     * @param contentLength the maximum number of bytes to copy, or -1 to read the InputStream fully.
     * @return true if the operation succeeded completely; false otherwise.
     */
    public static synchronized boolean copy(InputStream in, OutputStream out, int contentLength) {
        int length = (contentLength > 0) ? contentLength : Integer.MAX_VALUE;
        boolean result = true;
        int bytesRead = 0;
        try {
            byte[] b = new byte[4096];
            int n;
            while ((bytesRead < length) && ((n = in.read(b, 0, Math.min(b.length, length - bytesRead))) != -1)) {
                out.write(b, 0, n);
                bytesRead += n;
            }
            out.flush();
        } catch (Exception ex) {
            result = false;
        } finally {
            if (contentLength < 0)
                close(in);
            close(out);
        }
        return result;
    }

    /**
     * Close an InputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(InputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close an OutputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(OutputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Writer and ignore Exceptions.
     * @param writer the writer to close.
     */
    public static void close(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Reader and ignore Exceptions.
     * @param reader the reader to close.
     */
    public static void close(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a JarFile and ignore Exceptions.
     * @param jarFile the JarFile to close.
     */
    public static void close(JarFile jarFile) {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a ZipFile and ignore Exceptions.
     * @param zipFile the zipFile to close.
     */
    public static void close(ZipFile zipFile) {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (Exception ignore) {
            }
        }
    }
}

Related

  1. copyDirectory(File file, File folderDestino)
  2. copyDirectory(File from, File to)
  3. copyDirectory(File inDir, File outDir)
  4. copyDirectory(File inputFolder, File outputFolder)
  5. copyDirectory(File source, File destination)
  6. copyDirectory(File source, File destination)