Java Utililty Methods Directory Copy

List of utility methods to do Directory Copy

Description

The list of methods to do Directory Copy are organized into topic(s).

Method

voidcopyDirectory(File sourceLocation, File targetLocation)
copy Directory
if (!sourceLocation.exists())
    throw new IOException("File does not exist: " + sourceLocation);
if (sourceLocation.isDirectory()) {
    if (!targetLocation.exists() && !targetLocation.mkdir())
        throw new IOException("Unable to create directory: " + targetLocation);
    for (String child : sourceLocation.list())
        copyDirectory(new File(sourceLocation, child), new File(targetLocation, child));
} else {
...
voidcopyDirectory(File sourceLocation, File targetLocation)
copy Directory
if (sourceLocation.isDirectory()) {
    if (!targetLocation.exists()) {
        targetLocation.mkdirs();
    String[] children = sourceLocation.list();
    for (int i = 0; i < children.length; i++) {
        copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
} else {
    InputStream in = new FileInputStream(sourceLocation);
    OutputStream out = new FileOutputStream(targetLocation);
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    in.close();
    out.close();
voidcopyDirectory(File sourceLocation, File targetLocation)
Copy a directory recursively.
if (sourceLocation.isDirectory()) {
    if (!targetLocation.exists() && !targetLocation.mkdir()) {
        throw new IOException("Can not create temp. directory.");
    String[] children = sourceLocation.list();
    for (int i = 0; i < children.length; ++i) {
        copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
} else {
    copyFile(sourceLocation, targetLocation);
voidcopyDirectory(File sourceLocation, File targetLocation)
copy Directory
copyDirectory(sourceLocation, targetLocation, null);
voidcopyDirectory(File sourceLocation, File targetLocation, boolean overwrite)
Copy a file or directory to another.
if (sourceLocation.isDirectory()) {
    if (!targetLocation.exists()) {
        boolean created = targetLocation.mkdirs();
        if (!created) {
            System.out.println("FileUtil copyDirectory ERROR couldn't mkdirs:" + targetLocation);
            return;
    String[] children = sourceLocation.list();
    for (int i = 0; i < children.length; i++) {
        copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]),
                overwrite);
} else if (overwrite || !targetLocation.exists()) {
    InputStream in = new FileInputStream(sourceLocation);
    OutputStream out = new FileOutputStream(targetLocation);
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    in.close();
    out.close();
    if (sourceLocation.canExecute())
        targetLocation.setExecutable(true);
voidcopyDirectory(File sourceLocation, File targetLocation, int bufferSize)
Copy directory.
if (sourceLocation.isDirectory()) {
    if (!targetLocation.exists()) {
        targetLocation.mkdir();
    String[] children = sourceLocation.list();
    for (int i = 0; i < children.length; i++) {
        copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]),
                bufferSize);
...
voidcopyDirectory(File sourceLocation, File targetLocation, String[] fileExtensions)
Copies the directory from the source location to the target one if fileExtensions is null all subsequent files will be copied
try {
    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            org.eclipse.core.runtime.Assert.isTrue(targetLocation.mkdir());
        String[] children = sourceLocation.list();
        for (String child : children) {
            boolean eligibleByExtension = false;
...