Java Directory Copy copyDirectory(File sourceLocation, File targetLocation, String[] fileExtensions)

Here you can find the source of copyDirectory(File sourceLocation, File targetLocation, String[] fileExtensions)

Description

Copies the directory from the source location to the target one if fileExtensions is null all subsequent files will be copied

License

Open Source License

Parameter

Parameter Description
sourceLocation - source directory
targetLocation - target directory
fileExtensions the list of the file name filters, that specify which files to copy

Declaration

public static void copyDirectory(File sourceLocation, File targetLocation, String[] fileExtensions) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008, 2009 SOPERA GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://w  ww.  j  a v  a 2s .  c  om
 *     SOPERA GmbH - initial API and implementation
 *******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Copies the directory from the source location to the target one
     * if fileExtensions is null all subsequent files will be copied 
     * @param sourceLocation - source directory
     * @param targetLocation - target directory
     * @param fileExtensions the list of the file name filters, that specify which files to copy  
     */
    public static void copyDirectory(File sourceLocation, File targetLocation, String[] fileExtensions) {
        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;
                    if (fileExtensions != null) {
                        for (String fileExtension : fileExtensions) {
                            if (child.toLowerCase().endsWith(fileExtension.toLowerCase())) {
                                eligibleByExtension = true;
                                break;
                            }
                        }
                    } else {
                        eligibleByExtension = true;
                    }

                    if (eligibleByExtension) {
                        copyDirectory(new File(sourceLocation, child), new File(targetLocation, child),
                                fileExtensions);
                    }
                }
            } else {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = new FileInputStream(sourceLocation);
                    out = new FileOutputStream(targetLocation);

                    // Copy the bits from instream to outstream
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                } finally {
                    if (in != null) {
                        in.close();
                    }
                    if (out != null) {
                        out.close();
                    }
                }
            }
        } catch (Exception ex) {
            throw new IllegalStateException(ex.getMessage(), ex);
        }
    }
}

Related

  1. copyDirectory(File sourceLocation, File targetLocation)
  2. copyDirectory(File sourceLocation, File targetLocation)
  3. copyDirectory(File sourceLocation, File targetLocation)
  4. copyDirectory(File sourceLocation, File targetLocation, boolean overwrite)
  5. copyDirectory(File sourceLocation, File targetLocation, int bufferSize)