Java File list(FilenameFilter filter) method example

HQ ┬╗ Java Tutorial ┬╗ ┬╗ File ┬╗ list(FilenameFilter filter) method example
Java File list(FilenameFilter filter) method example 2018-02-16T07:26:41+00:00 list(FilenameFilter filter)


On this document we will be showing a java example on how to use the list(FilenameFilter filter)method of File Class. This method returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. The behavior of this method is the same as that of the list() method, except that the strings in the returned array must satisfy the filter. If the given filter is null then all names are accepted. Otherwise, a name satisfies the filter if and only if the value true results when the FilenameFilter.accept(File, String) method of the filter is invoked on this abstract pathname and the name of a file or directory in the directory that it denotes.


SecurityException – If a security manager exists and its SecurityManager.checkRead(String) method denies read access to the directory

Method Syntax

public String[] list(FilenameFilter filter)

Method Argument

Data Type Parameter Description
FilenameFilter filter A filename filter

Method Returns

This method returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that were accepted by the given filter. The array will be empty if the directory is empty or if no names were accepted by the filter. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.


Requires Java 1.0 and up

Java File list(FilenameFilter filter) Example

Below is a java code demonstrates the use of list(FilenameFilter filter) method of File class. The example presented might be simple however it shows the behaviour of the list(FilenameFilter filter) method of File class. Because the list(FilenameFilter filter) method returns an array of String filtered depending on the FilenameFilter as method argument, it is imperative that the abstract pathname is a directory. As such we have put a check isDirectory() to make sure that the pathname is a directory or not before proceeding with the operation.

Sample Output

Below is the sample output when you run the above example.

java lang File list(FilenameFilter filter) example output

Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.