Java - How to get a list of files in a specific directory

Here is how to print the file list of a specific directory.

Java`s File class provides the following APIs.

  • listFiles() : Lists files in directory path into an array.
  • listFiles(FilenameFilter filter) : Filters files in a directory path and returns the result as an array.
  • listFiles(FileFilter filter) : Filters files in a directory path and returns the result as an array.
  • list() : Lists the names of files in a directory path into an array.
  • list(FilenameFilter filter) : Filters the names of files in a directory path and returns the result as an array.

The directory to browse is my local folder /home/js/test/test, and the file structure of this path is as follows.

.
├── file1.txt
├── folder1
│   └── file2.txt
├── folder2
│   ├── file3.txt
│   └── file4.txt
├── folder3
│   ├── file5.txt
│   ├── file6.txt
│   └── folder4
│       ├── text2.txt
│       ├── text3.txt
│       └── text.txt
└── myscript.sh

Output a list of files (listFiles)

listFiles() returns a list of files immediately below the file as an array.

However, the files in all subfolders are not recursively imported.

When the following code is executed, the files under /home/js/test/test are output.

File dir = new File("/home/js/test/test");
File files[] = dir.listFiles();

for (int i = 0; i < files.length; i++) {
    System.out.println("file: " + files[i]);
}

result

file: /home/js/test/test/folder1
file: /home/js/test/test/file1.txt
file: /home/js/test/test/myscript.sh
file: /home/js/test/test/folder3
file: /home/js/test/test/folder2

Output a list of files (Recursive, listFiles)

You can recursively call listFiles() to print all files in subfolders as well.

The following code prints all files under /home/js/test/test.

System.out.println("2");
showFilesInDIr("/home/js/test/test");


public static void showFilesInDIr(String dirPath) {
    File dir = new File(dirPath);
    File files[] = dir.listFiles();

    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        if (file.isDirectory()) {
            showFilesInDIr(file.getPath());
        } else {
            System.out.println("file: " + file);
        }
    }
}

result

file: /home/js/test/test/folder1/file2.txt
file: /home/js/test/test/file1.txt
file: /home/js/test/test/myscript.sh
file: /home/js/test/test/folder3/file5.txt
file: /home/js/test/test/folder3/folder4/text3.txt
file: /home/js/test/test/folder3/folder4/text2.txt
file: /home/js/test/test/folder3/folder4/text.txt
file: /home/js/test/test/folder3/file6.txt
file: /home/js/test/test/folder2/file3.txt
file: /home/js/test/test/folder2/file4.txt

Output a list of files (listFiles(FilenameFilter))

If you use listFiles(FilenameFilter), you can output the results of filtering files by specific conditions.

The code below filters only files that start with "file" and returns them as a result.

File dir = new File("/home/js/test/test");
FilenameFilter filter = new FilenameFilter() {
    public boolean accept(File f, String name) {
        return name.startsWith("file");
    }
};

File files[] = dir.listFiles(filter);
for (int i = 0; i < files.length; i++) {
    System.out.println("file: " + files[i]);
}

Whenever a file is traversed, accept(File f, String name) is called, where File f is the files parent directory. That means /home/js/test/test. String name` is the name of the file.

result

file: /home/js/test/test/file1.txt

Output a list of files (listFiles(FileFilter))

FilenameFilter receives the name of the file as an argument and should filter by name only. If you want to receive files, you should use listFiles(FileFilter).

The following is the code for filtering with listFiles(FileFilter).

File dir = new File("/home/js/test/test");
FileFilter filter = new FileFilter() {
    public boolean accept(File f) {
        return f.getName().endsWith("txt");
    }
};

File files[] = dir.listFiles(filter);
for (int i = 0; i < files.length; i++) {
    System.out.println("file: " + files[i]);
}

result

file: /home/js/test/test/file1.txt

Output file list (Recursive, Filtering)

This example recursively traverses all subfolders and tries to find only files with certain conditions.

Only listFiles() was used, and the filtering condition was implemented directly without passing it as an argument such as FilenameFilter.

FileFilter filter = f -> f.getName().endsWith("txt");
showFilesInDIr2("/home/js/test/test");


public static void showFilesInDIr2(String dirPath) {
    File dir = new File(dirPath);
    File files[] = dir.listFiles();

    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        if (file.isDirectory()) {
            showFilesInDIr(file.getPath());
        } else if (file.getName().endsWith(".txt")){
            System.out.println("file: " + file);
        }
    }
}

If you look at the result, all files ending in .txt were printed.

file: /home/js/test/test/folder1/file2.txt
file: /home/js/test/test/file1.txt
file: /home/js/test/test/folder3/file5.txt
file: /home/js/test/test/folder3/folder4/text3.txt
file: /home/js/test/test/folder3/folder4/text2.txt
file: /home/js/test/test/folder3/folder4/text.txt
file: /home/js/test/test/folder3/file6.txt
file: /home/js/test/test/folder2/file3.txt
file: /home/js/test/test/folder2/file4.txt

Output file list (file name only)

list() returns an array of file names of sub-paths. If you don`t need a File object and just need the file name as a String, you can use this API.

File dir = new File("/home/js/test/test");
String[] filenames = dir.list();
for (int i = 0; i < filenames.length; i++) {
    System.out.println("file: " + filenames[i]);
}

result

file: folder1
file: file1.txt
file: myscript.sh
file: folder3
file: folder2

Output file list (file name only, filtering)

list(FilenameFilter filter) filters the file names by specific criteria and returns the result as an array.

File dir = new File("/home/js/test/test");
String[] filenames = dir.list((f,name)->name.endsWith(".txt"));
for (int i = 0; i < filenames.length; i++) {
    System.out.println("file: " + filenames[i]);
}

result

file: file1.txt

Reference

codechachaCopyright ©2019 codechacha