Java Recursive Copy recursiveCopy(File copyFromDir, File copyToDir, boolean overwrite)

Here you can find the source of recursiveCopy(File copyFromDir, File copyToDir, boolean overwrite)

Description

Will recursively copy the contents of copyFromDir to copyToDir creating any directories in the copyToDir that are necessary.

License

Apache License

Parameter

Parameter Description
copyFromDir the files and directories to recursively copy.
copyToDir the direcory to copy them to, if it doesn't exist, it will be created.
overwrite set to true if an existing copy of the file should be overwritten.

Declaration

public static void recursiveCopy(File copyFromDir, File copyToDir, boolean overwrite) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**/* w  w  w. jav  a 2s. c o  m*/
     * Will recursively copy the contents of copyFromDir to copyToDir
     * creating any directories in the copyToDir that are necessary.
     *
     * @param copyFromDir the files and directories to recursively copy.
     * @param copyToDir   the direcory to copy them to, if it doesn't exist, it will be created.
     * @param overwrite   Set to true if an existing file should be overwritten.
     */
    public static void recursiveCopy(String copyFromDir, String copyToDir, boolean overwrite) {
        File copyFrom = new File(copyFromDir);
        File copyTo = new File(copyToDir);
        recursiveCopy(copyFrom, copyTo, overwrite);
    }

    /**
     * Will recursively copy the contents of copyFromDir to copyToDir
     * creating any directories in the copyToDir that are necessary.
     *
     * @param copyFromDir the files and directories to recursively copy.
     * @param copyToDir   the direcory to copy them to, if it doesn't exist, it will be created.
     * @param overwrite   set to true if an existing copy of the file should be overwritten.
     */
    public static void recursiveCopy(File copyFromDir, File copyToDir, boolean overwrite) {
        try {
            //Create the copy to directory and any parent directories
            //if needed.
            copyToDir.mkdirs();

            File[] filesToCopy = copyFromDir.listFiles();
            File newDirectory;
            String slash = System.getProperty("file.separator");
            File newFile;

            for (int i = 0; i < filesToCopy.length; i++) {
                //If the File object represents a directory,
                //recursively copy all files in that dir.
                if (filesToCopy[i].isDirectory()) {
                    newDirectory = new File(copyToDir + slash + filesToCopy[i].getName());
                    recursiveCopy(filesToCopy[i], newDirectory, overwrite);
                } else {
                    newFile = new File(copyToDir.toString() + slash + filesToCopy[i].getName());
                    //Just copy the file to its new home.
                    //If overwrite copy the file,
                    //or if not overright, but the file doesn't exist
                    //copy it over.
                    if (overwrite || (!newFile.exists())) {
                        copyFile(filesToCopy[i].toString(),
                                copyToDir.toString() + slash + filesToCopy[i].getName());
                    } //if
                } //else
            } //for
        } catch (Exception e) {
            e.printStackTrace();
        } //catch
    }

    /**
     * { method
     *
     * @param dst destination file name
     * @param src source file name
     * @return true for success
     *         }
     * @name copyFile
     * @function copy file named src into new file named dst
     */
    public static boolean copyFile(String src, String dst) {
        int bufsize = 1024;
        try {
            RandomAccessFile srcFile = new RandomAccessFile(src, "r");
            long len = srcFile.length();
            if (len > 0x7fffffff) {
                return (false);
            }
            // too large
            int l = (int) len;
            if (l == 0) {
                return (false);
            }
            // failure - no data
            RandomAccessFile dstFile = new RandomAccessFile(dst, "rw");

            int bytesRead = 0;
            byte[] buffer = new byte[bufsize];
            while ((bytesRead = srcFile.read(buffer, 0, bufsize)) != -1) {
                dstFile.write(buffer, 0, bytesRead);
            }
            srcFile.close();
            dstFile.close();
            return true;
        } catch (IOException ex) {
            return (false);
        }
    }

    /**
     * { method
     *
     * @param dst destination file name
     * @param src source file name
     * @return true for success
     *         }
     * @name copyFile
     * @function copy file named src into new file named dst
     */
    public static boolean copyFile(File src, File dst) {
        int bufsize = 1024;
        try {
            RandomAccessFile srcFile = new RandomAccessFile(src, "r");
            long len = srcFile.length();
            if (len > 0x7fffffff) {
                return (false);
            }
            // too large
            int l = (int) len;
            if (l == 0) {
                return (false);
            }
            // failure - no data
            RandomAccessFile dstFile = new RandomAccessFile(dst, "rw");

            int bytesRead = 0;
            byte[] buffer = new byte[bufsize];
            while ((bytesRead = srcFile.read(buffer, 0, bufsize)) != -1) {
                dstFile.write(buffer, 0, bytesRead);
            }
            srcFile.close();
            dstFile.close();
            long srcDate = src.lastModified();
            dst.setLastModified(srcDate);
            return true;
        } catch (IOException ex) {
            return (false);
        }
    }
}

Related

  1. recursiveCopy(File sourceFile, File targetLocation)
  2. recursiveCopy(File src, File dest)
  3. recursiveCopyDir(File source, File destination)
  4. recursiveCopyFile(File srcDir, File destDir)