Java File Copy copyFiles(File srcDir, File destDir, String... regexps)

Here you can find the source of copyFiles(File srcDir, File destDir, String... regexps)

Description

Copies files from source to destination dir.

License

Open Source License

Parameter

Parameter Description
srcDir source directory
destDir destination directory
regexps regular expressions for file names

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFiles(File srcDir, File destDir, String... regexps) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w w  w . j a va2 s  .  com*/
 * This file is part of Pustefix.
 *
 * Pustefix is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Pustefix 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
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Pustefix; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

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

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * Copies files from source to destination dir. The files can be filtered by name using one
     * or more regular expressions (e.g. ".*gif", ".*jpg").
     * 
     * @param srcDir source directory
     * @param destDir destination directory
     * @param regexps regular expressions for file names
     * @throws IOException
     */
    public static void copyFiles(File srcDir, File destDir, String... regexps) throws IOException {
        if (!(srcDir.exists() && srcDir.isDirectory()))
            throw new IllegalArgumentException("Source directory doesn't exist: " + srcDir.getAbsolutePath());
        if (!(destDir.exists() && destDir.isDirectory()))
            throw new IllegalArgumentException("Destination directory doesn't exist: " + destDir.getAbsolutePath());
        File[] files = srcDir.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                String name = file.getName();
                boolean matches = true;
                for (String regexp : regexps) {
                    matches = name.matches(regexp);
                    if (matches)
                        break;
                }
                if (matches) {
                    File destFile = new File(destDir, file.getName());
                    copyFile(file, destFile);
                }
            }
        }
    }

    /**
     * Copies a source file to a target file.
     * 
     * @param srcFile source file
     * @param destFile target file
     * @throws IOException
     */
    public static void copyFile(File srcFile, File destFile) throws IOException {
        if (!(srcFile.exists() && srcFile.isFile()))
            throw new IllegalArgumentException("Source file doesn't exist: " + srcFile.getAbsolutePath());
        if (destFile.exists() && destFile.isDirectory())
            throw new IllegalArgumentException("Destination file is directory: " + destFile.getAbsolutePath());
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        byte[] buffer = new byte[4096];
        int no = 0;
        try {
            while ((no = in.read(buffer)) != -1)
                out.write(buffer, 0, no);
        } finally {
            in.close();
            out.close();
        }
    }
}

Related

  1. copyFiles(File src, File dest)
  2. copyFiles(File src, File dest)
  3. copyFiles(File src, File dest, boolean override)
  4. copyFiles(File src, File dest, FilenameFilter filter)
  5. copyFiles(File src, File dest, String... filenameExtension)
  6. copyFiles(File srcFile, File destFolder)
  7. copyFiles(InputStream inStream, FileOutputStream outStream)
  8. copyFiles(String from, String to)
  9. copyFiles(String fromDirName, String toDirName)