Java Copy Directory copyDirectory(File srcFile, File destFile)

Here you can find the source of copyDirectory(File srcFile, File destFile)

Description

copy Directory

License

Open Source License

Declaration

public static int copyDirectory(File srcFile, File destFile) 

Method Source Code

//package com.java2s;
/*/*from w w w. j  a v  a2s.c o  m*/
 This file is part of the BlueJ program. 
 Copyright (C) 1999-2009,2014  Michael Kolling and John Rosenberg 
     
 This program is free software; you can redistribute it and/or 
 modify it under the terms of the GNU General Public License 
 as published by the Free Software Foundation; either version 2 
 of the License, or (at your option) any later version. 
     
 This program 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 General Public License for more details. 
     
 You should have received a copy of the GNU General Public License 
 along with this program; if not, write to the Free Software 
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
     
 This file is subject to the Classpath exception as provided in the  
 LICENSE.txt file that accompanied this code.
 */

import java.io.*;

public class Main {
    /**
     * Copy (recursively) a whole directory.
     */
    public static final int NO_ERROR = 0;
    public static final int SRC_NOT_DIRECTORY = 2;
    public static final int COPY_ERROR = 3;
    public static final int DEST_EXISTS_NOT_DIR = 4;
    public static final int DEST_EXISTS_NON_EMPTY = 5;

    public static int copyDirectory(File srcFile, File destFile) {
        if (!srcFile.isDirectory())
            return SRC_NOT_DIRECTORY;

        if (destFile.exists() && !destFile.isDirectory())
            return DEST_EXISTS_NOT_DIR;

        // It's okay if it exists ,provided it is empty:
        if (destFile.exists()) {
            if (destFile.list().length > 0)
                return DEST_EXISTS_NON_EMPTY;
        } else {
            if (!destFile.mkdir())
                return COPY_ERROR;
        }

        String[] dir = srcFile.list();
        for (int i = 0; i < dir.length; i++) {
            //String srcName = source + File.separator + dir[i];
            File file = new File(srcFile, dir[i]);
            if (file.isDirectory()) {
                if (copyDirectory(file, new File(destFile, dir[i])) != NO_ERROR)
                    return COPY_ERROR;
            } else {
                File file2 = new File(destFile, dir[i]);
                try {
                    copyFile(file, file2);
                } catch (IOException ioe) {
                    return COPY_ERROR;
                }
            }
        }
        return NO_ERROR;
    }

    /**
     * Copy file 'source' to file 'dest'. The source file must exist,
     * the destination file will be created. Returns true if successful.
     */
    public static void copyFile(String source, String dest) throws IOException {
        File srcFile = new File(source);
        File destFile = new File(dest);

        copyFile(srcFile, destFile);
    }

    /**
     * Copy file 'srcFile' to file 'destFile'. The source file must exist,
     * the destination file will be created. Returns true if successful.
     */
    public static void copyFile(File srcFile, File destFile) throws IOException {
        // check whether source and dest are the same
        if (srcFile.equals(destFile)) {
            return; // don't bother - they are the same
        }

        InputStream in = null;
        OutputStream out = null;

        try {
            in = new BufferedInputStream(new FileInputStream(srcFile));
            out = new BufferedOutputStream(new FileOutputStream(destFile));
            copyStream(in, out);
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * Copy stream 'in' to stream 'out'.
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        for (int c; (c = in.read()) != -1;)
            out.write(c);
    }
}

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 srcDir, File dstDir)
  6. copyDirectory(File srcPath, File dstPath)
  7. copyDirectory(File srcPath, File dstPath)
  8. copyDirectory(final File sourceDir, final File destDir)
  9. copyDirectory(final File src, final File dest)