Here you can find the source of getAllFilesInDirMatchingPattern(String directory, final String regex, final boolean first_match2)
public static List<File> getAllFilesInDirMatchingPattern(String directory, final String regex, final boolean first_match2)
//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()); } }