Java FileInputStream Copy copyFileRecursive(File src, File dest)

Here you can find the source of copyFileRecursive(File src, File dest)

Description

This function will copy files or directories from one location to another.

License

Open Source License

Parameter

Parameter Description
src -- A File object that represents the source for the copy
dest -- A File object that represents the destination for the copy.

Exception

Parameter Description
IOException if unable to copy.

Declaration

public static void copyFileRecursive(File src, File dest) throws IOException 

Method Source Code

//package com.java2s;
/*//from w  w w .  ja v a  2s  . co m
 * Copyright (C) 2013 Universitat Pompeu Fabra
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * This function will copy files or directories from one location to
     * another. note that the source and the destination must be mutually
     * exclusive. This function can not be used to copy a directory to a sub
     * directory of itself. The function will also have problems if the
     * destination files already exist.
     *
     * @param src -- A File object that represents the source for the copy
     * @param dest -- A File object that represents the destination for the
     * copy.
     * @throws IOException if unable to copy.
     */
    public static void copyFileRecursive(File src, File dest) throws IOException {
        // Check to ensure that the source is valid...
        if (!src.exists()) {
            throw new IOException("copyFiles: Can not find source: " + src.getAbsolutePath() + ".");
        } else if (!src.canRead()) { //check to ensure we have rights to the source...
            throw new IOException("copyFiles: No right to source: " + src.getAbsolutePath() + ".");
        }
        // is this a directory copy?
        if (src.isDirectory()) {
            // does the destination already exist?
            // if not we need to make it exist if possible
            // (NOTE this is mkdirs not mkdir)
            if (!dest.exists() && !dest.mkdirs()) {
                throw new IOException("copyFiles: Could not create direcotry: " + dest.getAbsolutePath() + ".");
            }
            // get a listing of files...
            String[] list = src.list();
            for (String listEntry : list) {
                File dest1 = new File(dest, listEntry);
                File src1 = new File(src, listEntry);
                copyFileRecursive(src1, dest1);
            }
        } else {
            // This was not a directory, so lets just copy the file
            FileInputStream fin = null;
            FileOutputStream fout = null;
            byte[] buffer = new byte[4096]; // Buffer 4K at a time (you can change this).
            int bytesRead;
            try {
                // open the files for input and output
                fin = new FileInputStream(src);
                fout = new FileOutputStream(dest);
                // while bytesRead indicates a successful read, lets write...
                while ((bytesRead = fin.read(buffer)) >= 0) {
                    fout.write(buffer, 0, bytesRead);
                }
            } catch (IOException ex) { // Error copying file...
                IOException wrapper = new IOException("copyFiles: Unable to copy file: " + src.getAbsolutePath()
                        + "to" + dest.getAbsolutePath() + ".");
                wrapper.initCause(ex);
                wrapper.setStackTrace(ex.getStackTrace());
                throw wrapper;
            } finally { // Ensure that the files are closed (if they were open).
                if (fin != null) {
                    fin.close();
                }
                if (fout != null) {
                    fout.close();
                }
            }
        }
    }
}

Related

  1. copyFileFolder(final File source, final File dest, final boolean override)
  2. copyFileFromFileToFile(File fromFile, File toFile)
  3. copyFileFromTo(String filename, File fromDir, File toDir)
  4. copyFileLowLevel(File source, File target)
  5. copyFileR(File srcFile, File destDirectory)
  6. copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile)
  7. copyFileToContainer(File pChild, IContainer pContainer)
  8. copyFileToDestDir(String srcFilePath, String destFileDir)
  9. copyFileToDir(File oldFile, File outputDir)