Java Copy Directory copyDirectoryContents(File directory, String targetDirName, boolean update)

Here you can find the source of copyDirectoryContents(File directory, String targetDirName, boolean update)

Description

Copies the contents of a directory to the specified target directory.

License

Open Source License

Parameter

Parameter Description
directory the directory containing files
targetDirName the directory to which the files should be copied to
update is true when files should be only copied when the source files are newer compared to the target files.

Exception

Parameter Description
IOException when a file could not be copied
IllegalArgumentException when the directory is not a directory.

Declaration

public static void copyDirectoryContents(File directory, String targetDirName, boolean update)
        throws IOException 

Method Source Code

//package com.java2s;
/*//from   w w  w .  j  a v  a  2 s.  c o  m
 * Copyright (C) 2010 Viettel Telecom. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

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 {
    /** .*/
    private static final int COPY_BUFFER_SIZE = 1024 * 1024;

    /**
     * Copies the contents of a directory to the specified target directory.
     *
     * @param directory the directory containing files
     * @param targetDirName the directory to which the files should be copied to
     * @param update is true when files should be only copied when the source files are
     * newer compared to the target files.
     * @throws IOException when a file could not be copied
     * @throws IllegalArgumentException when the directory is not a directory.
     */
    public static void copyDirectoryContents(File directory, String targetDirName, boolean update)
            throws IOException {
        copyDirectoryContents(directory, new File(targetDirName), update);
    }

    /**
     * Copies the contents of a directory to the specified target directory.
     *
     * @param directory the directory containing files
     * @param targetDir the directory to which the files should be copied to
     * @param update is true when files should be only copied when the source files
     * are newer compared to the target files.
     * @throws IOException when a file could not be copied
     * @throws IllegalArgumentException when the directory is not a directory.
     */
    public static void copyDirectoryContents(File directory, File targetDir, boolean update) throws IOException {
        if (!directory.isDirectory()) {
            throw new IllegalArgumentException("Cannot copy contents of the file [" + directory.getAbsolutePath()
                    + "]: specify a directory instead.");
        }
        String[] fileNames = directory.list();
        for (int i = 0; i < fileNames.length; i++) {
            String fileName = fileNames[i];
            File file = new File(directory.getAbsolutePath(), fileName);
            if (file.isDirectory()) {
                copyDirectoryContents(file, targetDir.getAbsolutePath() + File.separatorChar + fileName, update);
            } else {
                File targetFile = new File(targetDir, fileName);
                if (update) {
                    // update only when the source file is newer:
                    if ((!targetFile.exists()) || (file.lastModified() > targetFile.lastModified())) {
                        copy(file, targetFile);
                    }
                } else {
                    // copy the file in all cases:
                    copy(file, targetFile);
                }
            }
        }
    }

    /**
     * Copies the given files to the specified target directory.
     * @param files The files which should be copied, when an array element is null, it will be ignored.
     * @param targetDir The directory to which the given files should be copied to.
     * @throws IOException when there is an error while copying the file.
     */
    public static void copy(File[] files, File targetDir) throws IOException {
        copy(files, targetDir, false);
    }

    /**
     * Copies the given files to the specified target directory.
     *
     * @param files The files which should be copied, when an array element is null, it will be ignored.
     * @param targetDir The directory to which the given files should be copied to.
     * @param overwrite true when existing target files should be overwritten even when they are newer
     * @throws IOException when there is an error while copying the file.
     */
    public static void copy(File[] files, File targetDir, boolean overwrite) throws IOException {
        String targetPath = targetDir.getAbsolutePath() + File.separatorChar;
        byte[] buffer = new byte[COPY_BUFFER_SIZE];
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file != null) {
                File targetFile = new File(targetPath + file.getName());
                if (!overwrite && targetFile.exists() && targetFile.lastModified() > file.lastModified()) {
                    continue;
                }
                copy(file, targetFile, buffer);
            }
        }
    }

    /**
     * Copies a file.
     *
     * @param source The file which should be copied
     * @param target The file or directory to which the source-file should be copied to.
     * @throws FileNotFoundException when the source file was not found
     * @throws IOException when there is an error while copying the file.
     */
    public static void copy(File source, File target) throws IOException {
        copy(source, target, new byte[1024 * 1024]);
    }

    /**
     * Copies a file.
     *
     * @param source The file which should be copied
     * @param target The file or directory to which the source-file should be copied to.
     * @param buffer A buffer used for the copying.
     * @throws FileNotFoundException when the source file was not found
     * @throws IOException when there is an error while copying the file.
     */
    private static void copy(File source, File target, byte[] buffer) throws IOException {
        InputStream in = new FileInputStream(source);
        // create parent directory of target-file if necessary:
        File parent = target.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        if (target.isDirectory()) {
            target = new File(target, source.getName());
        }
        OutputStream out = new FileOutputStream(target);
        int read;
        try {
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            in.close();
            out.close();
        }
    }
}

Related

  1. copyDirectory(final String sourceDir, final String targetDir)
  2. copyDirectory(String sourceDir, String targetDir)
  3. copyDirectory(String srcDir, String destDir)
  4. copyDirectoryAndOverwriteFilesIfNeeded(File srcDir, File destDir)
  5. copyDirectoryContent(File srcPath, File targetPath)
  6. copyDirectoryContents(File source, File destination)
  7. copyDirectoryContents(File src, File target)
  8. copyDirectoryContents(File srcDir, File dstDir)
  9. copyDirectoryFromJar(String jarName, String srcDir, File tmpDir)