Java Utililty Methods Directory to File List

List of utility methods to do Directory to File List

Description

The list of methods to do Directory to File List are organized into topic(s).

Method

String[]getAllFilesInFolder(String folder)
get All Files In Folder
File file = load(folder);
if (file != null) {
    if (file.exists()) {
        if (file.isDirectory()) {
            File[] fileList = file.listFiles();
            String[] fileNames = new String[fileList.length];
            for (int i = 0; i < fileNames.length; i++) {
                fileNames[i] = fileList[i].getName();
...
String[]getAllFilesInFolderAndSubFolders(String folder)
get All Files In Folder And Sub Folders
List<File> filesToCheck = new ArrayList<File>();
File file = load(folder);
if (file != null) {
    if (file.exists()) {
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                filesToCheck.add(f);
    boolean foldersInList = true;
    while (foldersInList) {
        List<File> remove = new ArrayList<File>();
        List<File> toCheck = new ArrayList<File>();
        for (File f : filesToCheck) {
            if (f.isDirectory()) {
                remove.add(f);
                for (File fi : f.listFiles()) {
                    toCheck.add(fi);
        for (File f : toCheck) {
            filesToCheck.add(f);
        for (File f : remove) {
            filesToCheck.remove(f);
        if (remove.size() == 0) {
            foldersInList = false;
    String[] files = new String[filesToCheck.size()];
    for (int i = 0; i < files.length; i++) {
        files[i] = filesToCheck.get(i).getPath();
    return files;
return new String[] {};
File[]getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)
get All Files In Hierarchy
return getAllFiles(new File(basePath), filter);
ListgetAllFilesInSubFolder(String base, String ending)
Scan the base folder, and return a list of all files with the given name ending
File dir = new File(base);
if (!dir.exists() || !dir.isDirectory()) {
    throw new IllegalArgumentException("Invalid folder " + base);
List<File> files = new LinkedList<File>();
recursiveFileSearch(files, dir, ending);
return files;
voidgetAllFilesInternal(final File aPath, final FilenameFilter filter, final List fileList)
get All Files Internal
final File[] allFiles = aPath.listFiles(filter);
for (int i = 0; i < allFiles.length; i++) {
    if (allFiles[i].isDirectory())
        getAllFilesInternal(allFiles[i], filter, fileList);
    else
        fileList.add(allFiles[i]);
VectorgetAllFilesLeastRecentFirst(File directory)
get All Files Least Recent First
Vector sortedFileList = new Vector();
if (!directory.exists())
    return sortedFileList;
File[] allFilesAndDirectories = directory.listFiles();
for (int i = 0; i < allFilesAndDirectories.length; ++i) {
    File fileToAdd = allFilesAndDirectories[i];
    if (!fileToAdd.isFile())
        continue;
...
ListgetAllFilesMatching(File srcDir, final String regex)
get All Files Matching
List<File> foundFiles = new ArrayList<File>();
if (!srcDir.isDirectory()) {
    if (srcDir.getPath().matches(regex)) {
        foundFiles.add(srcDir);
        return foundFiles;
    return foundFiles;
FilenameFilter filenameFiler = new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.matches(regex);
    };
};
foundFiles.addAll(Arrays.asList(srcDir.listFiles(filenameFiler)));
FileFilter fileFiler = new FileFilter() {
    public boolean accept(File file) {
        return file.isDirectory();
    };
};
File[] directories = srcDir.listFiles(fileFiler);
for (File file : directories) {
    foundFiles.addAll(getAllFilesMatching(file, regex));
return foundFiles;
String[]getAllFilesMatchingThisPatternIgnoreCase(String sDirectoryPath, String sPattern)
Returns the set of all files in the given directory, matching the given pattern (ignoring case)

String[] asAllFiles = getAllFiles(sDirectoryPath);
Vector vsFiltered = new Vector();
for (int i = 0; i < asAllFiles.length; i++) {
    if (asAllFiles[i].toLowerCase().matches(sPattern.toLowerCase())) {
        vsFiltered.add(asAllFiles[i]);
String[] asFilteredFiles = new String[vsFiltered.size()];
...
ListgetAllFilesPresentInFolder(File srcPath)
get All Files Present In Folder
List fileList = new ArrayList();
if (srcPath.isDirectory()) {
    String files[] = srcPath.list();
    for (int i = 0; i < files.length; i++) {
        fileList.addAll(getAllFilesPresentInFolder(new File(srcPath, files[i])));
} else {
    fileList.add(srcPath.getAbsolutePath());
...
voidgetAllFilesRecursively(File dir, List filelist)
get All Files Recursively
File[] files = dir.listFiles();
if (files == null) {
    return;
int i;
for (i = 0; i < files.length; ++i) {
    if (files[i].isFile()) {
        filelist.add(files[i]);
...