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

ListgetAllFilesIn(File dir, final String type)
get All Files In
return Arrays.asList(dir.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File arg0, String arg1) {
        return arg1.endsWith(type);
}));
ListgetAllFilesInAllSubDirectories(File parentDir, FileFilter filter)
Returns all the files that satisfy the filter criteria wherever (in any replace-directory) they are inside this folder
List<File> filesList = null;
return getAllFilesInAllSubDirectories(filesList, parentDir, filter);
ListgetAllFilesInDir(boolean traverseSubDirs, File dir, boolean includeHidden, String prefix)
get All Files In Dir
List<String> files = new ArrayList<String>();
String fileNames[] = dir.list();
for (String fileName : fileNames) {
    File currentFile = new File(dir, fileName);
    if (currentFile.isHidden() && !includeHidden) {
        continue;
    if (currentFile.isDirectory()) {
...
ListgetAllFilesInDirectory(File dir)
get All Files In Directory
return getAllFilesInDirectory(dir, true);
voidgetAllFilesInDirectory(File directory, List files)
get All Files In Directory
File[] fList = directory.listFiles();
for (File f : fList) {
    if (f.isFile()) {
        files.add(f);
    } else if (f.isDirectory()) {
        getAllFilesInDirectory(f, files);
ListgetAllFilesInDirectory(String directoryPath)
get All Files In Directory
return getAllFilesInDirectory(directoryPath, null);
ListgetAllFilesInDirectory(String dirName)
get All Files In Directory
File folder = new File(dirName);
File[] listOfFiles = folder.listFiles();
List<File> result = new ArrayList<>();
for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
        result.add(listOfFiles[i]);
return result;
File[]getAllFilesInDirectory(String path)
Returns the entire contents of a folder.
File folder = new File(path);
if (folder.exists()) {
    return folder.listFiles();
return null;
ListgetAllFilesInDirMatchingPattern(String directory, final String regex, final boolean first_match2)
get All Files In Dir Matching Pattern
File directoryFile = new File(directory);
checkDirectory(directoryFile);
FilenameFilter fileFilter = new FilenameFilter() {
    private boolean matchedyet = false;
    public boolean accept(File dir, String name) {
        if (first_match2 && matchedyet)
            return false;
        else {
...
ListgetAllFilesInFolder(File rootFolder, FileFilter filter, int maxFilesRequired)
Retrieves a List with at most maxFiles files that are in a folder or any of the subfolders in that folder satisfying a condition.
List<File> finalFiles = new ArrayList<>();
if (!Thread.currentThread().isInterrupted()) {
    if (maxFilesRequired < 0)
        throw new IllegalArgumentException("maxFilesRequired argument less than zero");
    if (rootFolder == null || filter == null)
        throw new IllegalArgumentException("folder or filter null");
    if (!rootFolder.exists() || !rootFolder.isDirectory())
        throw new IllegalArgumentException("rootFolder argument is not a directory");
...