Java Directory to File List getAllFilesInDirMatchingPattern(String directory, final String regex, final boolean first_match2)

Here you can find the source of getAllFilesInDirMatchingPattern(String directory, final String regex, final boolean first_match2)

Description

get All Files In Dir Matching Pattern

License

Open Source License

Declaration

public static List<File> getAllFilesInDirMatchingPattern(String directory, final String regex,
            final boolean first_match2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FilenameFilter;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<File> getAllFilesInDirMatchingPattern(String directory, final String regex,
            final boolean first_match2) {
        File directoryFile = new File(directory);
        checkDirectory(directoryFile);/* ww w .  ja v a 2 s. c om*/

        FilenameFilter fileFilter = new FilenameFilter() {
            private boolean matchedyet = false;

            public boolean accept(File dir, String name) {
                if (first_match2 && matchedyet)
                    return false;
                else {
                    boolean matched = name.matches(regex);
                    if (matched) {
                        matchedyet = matched;
                    }
                    return matched;
                }
            }
        };
        try {
            return listFiles(directoryFile, fileFilter);
        } catch (RuntimeException e) {
            throw new RuntimeException("problem adding files matching [" + regex + "] in dir " + directory, e);
        }
    }

    private static void checkDirectory(File directory) {
        if (!directory.isDirectory()) {
            throw new IllegalArgumentException(directory.getAbsolutePath() + " is not a directory");
        }
    }

    private static List<File> listFiles(File directory, FilenameFilter fileFilter) {
        checkDirectory(directory);
        List<File> matchingFiles = new ArrayList<File>();
        for (File file : directory.listFiles()) {
            if (passesFilter(directory, fileFilter, file)) {
                matchingFiles.add(file);
            }
            if (file.isDirectory()) {
                matchingFiles.addAll(listFiles(file, fileFilter));
            }
        }
        return matchingFiles;
    }

    private static boolean passesFilter(File directory, FilenameFilter fileFilter, File file) {
        return fileFilter == null || fileFilter.accept(directory, file.getName());
    }
}

Related

  1. getAllFilesInDirectory(File dir)
  2. getAllFilesInDirectory(File directory, List files)
  3. getAllFilesInDirectory(String directoryPath)
  4. getAllFilesInDirectory(String dirName)
  5. getAllFilesInDirectory(String path)
  6. getAllFilesInFolder(File rootFolder, FileFilter filter, int maxFilesRequired)
  7. getAllFilesInFolder(String folder)
  8. getAllFilesInFolderAndSubFolders(String folder)
  9. getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)