Java Utililty Methods Folder Read by Filter

List of utility methods to do Folder Read by Filter

Description

The list of methods to do Folder Read by Filter are organized into topic(s).

Method

ListgetFiles(File dir, FilenameFilter filter)
get Files
List<File> result = new ArrayList<File>();
getFiles(dir, result, filter);
return result;
ListgetFiles(File folder, FilenameFilter filter)
get Files
return getFiles(folder, filter, true);
ListgetFiles(File root, boolean recurse, FileFilter fileFilter)
get Files
List<File> result = new LinkedList<File>();
if (root.isDirectory()) {
    for (File file : root.listFiles()) {
        if (file.isDirectory() && recurse) {
            result.addAll(getFiles(file, recurse, fileFilter));
        } else if (file.isFile() && fileFilter.accept(file)) {
            result.add(file);
} else {
    result.add(root);
return result;
ListgetFiles(final File directory, final FilenameFilter filter)
get Files
if (directory.isDirectory()) {
    final File[] files = directory.listFiles(filter);
    if (files == null) {
        return Collections.emptyList();
    } else {
        return Arrays.asList(files);
} else {
...
ListgetFiles(String name, FileFilter filter, boolean includeSubtree)
Returns a list of all filtered files in the RFS.

If the name is not a folder the folder that contains the given file will be used instead.

Despite the filter may not accept folders, every subfolder is traversed if the includeSubtree parameter is set.

List<File> ret = new ArrayList<File>();
File file = new File(name);
if (!file.isDirectory()) {
    file = new File(file.getParent());
    if (!file.isDirectory()) {
        return ret;
File[] dirContent = file.listFiles();
for (int i = 0; i < dirContent.length; i++) {
    File f = dirContent[i];
    if (filter.accept(f)) {
        ret.add(f);
    if (includeSubtree && f.isDirectory()) {
        ret.addAll(getFiles(f.getAbsolutePath(), filter, true));
return ret;
ListgetFiles(String sourceDirPath, FilenameFilter filter, Comparator comparator)
get Files
File file = new File(sourceDirPath);
if (!file.exists()) {
    throw new IOException("Directory " + sourceDirPath + " does not exist");
if (!file.isDirectory()) {
    throw new IOException("File " + sourceDirPath + " is not a directory");
String[] files = file.list(filter);
...
ArrayListgetFiles(String sourceName, FilenameFilter filter)
Get the list of files contained in parent parameter directory
File sourceFile = new File(sourceName);
ArrayList result = new ArrayList();
if (!sourceFile.exists() || !sourceFile.isDirectory())
    return result;
String[] fileNames = sourceFile.list(filter);
if (fileNames == null)
    return result;
for (int i = 0; i < fileNames.length; i++) {
...
FileFiltergetFilesOnlyFilter()
get Files Only Filter
return filesOnlyFilter;
ListgetFilesRealWorking(File aStartingDir, FilenameFilter filter)
get Files Real Working
List<File> result = new ArrayList<File>();
File[] filesAndDirs = aStartingDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
Iterator filesIter = filesDirs.iterator();
File file = null;
while (filesIter.hasNext()) {
    file = (File) filesIter.next();
    if (filter.accept(aStartingDir, file.getName())) {
...
ListgetFilesUnder(String path, FileFilter fileFilter)
get Files Under
File root = new File(path);
List<File> files = new ArrayList<File>();
addFilesUnder(root, files, fileFilter);
return files;