java.io.File list(FilenameFilter filter)

Description

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.

Throws:

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.

Compatibility

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

Suggested Reading List

References