Java Directory to File List getAllFiles(File dir, Pattern filter)

Here you can find the source of getAllFiles(File dir, Pattern filter)

Description

Gets all files below the given directory.

License

Open Source License

Parameter

Parameter Description
filter file filter that determines the files to find
dir base directory

Return

all files below the base directory as File objects

Declaration

public static List<File> getAllFiles(File dir, Pattern filter) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.io.File;

import java.util.ArrayList;

import java.util.List;

import java.util.logging.Logger;
import java.util.regex.Pattern;

public class Main {
    /** jdk1.4 logger */
    private static Logger logger = Logger.getLogger("de.axelwernicke.mypod.util");

    /**/*from w w w. ja v  a2 s .  c  om*/
     * Gets all files below the given directory.
     *
     * @param filter file filter that determines the files to find
     * @param dir base directory
     * @return all files below the base directory as File objects
     */
    public static List<File> getAllFiles(File dir, Pattern filter) {
        logger.entering("de.axelwernicke.mypod.util.FileUtils", "getAllFiles");

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

        try {
            // determine all subdirectories
            File[] files = dir.listFiles();
            int fileLength = files.length;
            for (int i = 0; i < fileLength; i++) {
                if (files[i].isDirectory()) {
                    allFiles.addAll(getAllFiles(files[i], filter));
                } else {
                    if (filter.matcher(files[i].getPath()).matches()) {
                        allFiles.add(files[i]);
                    }
                }
            }
        } catch (Exception e) {
            logger.warning("exception raised: " + e.getMessage());
            e.printStackTrace();
        }

        logger.exiting("de.axelwernicke.mypod.util.FileUtils", "getAllFiles");

        return allFiles;
    }
}

Related

  1. getAllFilePath(String filedir)
  2. getAllFiles(File dir)
  3. getAllFiles(File dir)
  4. getAllFiles(File dir, List fileList)
  5. getAllFiles(File dir, List fileList)
  6. getAllFiles(File directory)
  7. getAllFiles(File directory)
  8. getAllFiles(File directory, boolean hidden)
  9. getAllFiles(File directory, String rootPath)