Java File Copy copyFiles(File src, File dest, String... filenameExtension)

Here you can find the source of copyFiles(File src, File dest, String... filenameExtension)

Description

This method copies, from a given directory, a set of files with a given list of file extensions.

License

Open Source License

Parameter

Parameter Description
src the root directory from which files are copied.
dest the destination directory in which files are copied.
filenameExtension a list of file extension to find which file to copy.

Declaration

public static void copyFiles(File src, File dest, String... filenameExtension) throws IOException 

Method Source Code

//package com.java2s;
/**//from  w  w  w.j av  a 2  s .co m
 * AADL-RAMSES
 * 
 * Copyright ? 2014 TELECOM ParisTech and CNRS
 * 
 * TELECOM ParisTech/LTCI
 * 
 * Authors: see AUTHORS
 * 
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the Eclipse Public License as published by Eclipse,
 * either version 1.0 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
 * Eclipse Public License for more details.
 * You should have received a copy of the Eclipse Public License
 * along with this program.  If not, see 
 * http://www.eclipse.org/org/documents/epl-v10.php
 */

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

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * This method copies, from a given directory, a set
     * of files with a given list of file extensions.
     * @param src the root directory from which files are copied.
     * @param dest the destination directory in which files are copied.
     * @param filenameExtension a list of file extension to find which file
     * to copy.
     */
    public static void copyFiles(File src, File dest, String... filenameExtension) throws IOException {

        List<File> files = new ArrayList<File>();

        if (src.isDirectory() == false) {
            return;
        } else {
            files.add(src);

            while (files.isEmpty() == false) {

                File tmpFile = files.remove(files.size() - 1);

                for (File subFile : tmpFile.listFiles()) {
                    if (subFile.isDirectory()) {
                        files.add(subFile);
                    } else {
                        for (String extension : filenameExtension) {
                            if (subFile.getName().endsWith(extension)) {
                                copyFile(subFile, new File(dest, subFile.getName()));
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * This method copies the content of a file (possibly directory)
     * in another file. Both must be created before calling this 
     * method.
     * @param src the source file that will be copied.
     * @param dest the destination file, result of the copy.
     * @throws for any IO problems
     */
    public static void copyFile(File src, File dest) throws IOException {
        InputStream in = null;
        OutputStream out = null;

        if (dest.isDirectory())
            dest = new File(dest, src.getName());

        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(dest);

            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();
            }
        }
    }
}

Related

  1. copyFiles(File sourceFile, File targetDir)
  2. copyFiles(File src, File dest)
  3. copyFiles(File src, File dest)
  4. copyFiles(File src, File dest, boolean override)
  5. copyFiles(File src, File dest, FilenameFilter filter)
  6. copyFiles(File srcDir, File destDir, String... regexps)
  7. copyFiles(File srcFile, File destFolder)
  8. copyFiles(InputStream inStream, FileOutputStream outStream)
  9. copyFiles(String from, String to)