FilenameFilter - sample program in Java

By: Reema sen  

You will often want to limit the number of files returned by the list() method to include only those files that match a certain filename pattern, or filter. To do this, you must use a second form of list(), shown here:

String[ ] list(FilenameFilter FFObj)

In this form, FFObj is an object of a class that implements the FilenameFilter interface. FilenameFilter defines only a single method, accept(), which is called once for each file in a list. Its general form is given here:

boolean accept(File directory, String filename)

The accept( ) method returns true for files in the directory specified by directory that should be included in the list (that is, those that match the filename argument), and returns false for those files that should be excluded.

The OnlyExt class, shown next, implements FilenameFilter. It will be used to modify the preceding program so that it restricts the visibility of the filenames returned by list( ) to files with names that end in the file extension specified when the object is constructed.

public class OnlyExt implements FilenameFilter {
String ext;
public OnlyExt(String ext) {
this.ext = "." + ext;
public boolean accept(File dir, String name) {
return name.endsWith(ext);

The following program will only display files that use the .html extension.

// Directory of .HTML files.
class DirListOnly {
public static void main(String args[]) {
String dirname = "/java";
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt("html");
String s[] = f1.list(only);
for (int i=0; i < s.length; i++) {

This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".

Archived Comments

1. while i am executing this program i am getting NullPointerException by using throws i added this exc
View Tutorial          By: Seema at 2013-11-26 18:39:15

2. while i am executing this program i am getting NullPointerException by using throws i added this exc
View Tutorial          By: Seema at 2013-11-26 18:32:52

3. thanks..
View Tutorial          By: Nipam at 2013-05-12 18:00:00

4. Hello, I liked the Tutorial, but it looks exactly like the FilenameFilter program in Herbert Schildt
View Tutorial          By: Srijan at 2013-03-15 10:15:08

5. you can put the a loop for multiple extension
View Tutorial          By: Arun at 2012-12-07 05:23:47

6. Hi,thanks I have implemented it in my programs as a lib_common
View Tutorial          By: Alvin567 at 2012-09-09 08:46:39

7. Hello,
this is good example, but that is only for one extension, how about multiple extension

View Tutorial          By: Zubair at 2011-11-10 20:46:39

8. Lots of thanks for this example
View Tutorial          By: Santosh at 2011-06-28 16:42:25

9. how to get this list to be alphabetically correct?
works on mac, not on linux :)

View Tutorial          By: Vincent at 2011-06-14 07:36:58

10. a very simple example that is very easy to follow and understand.

Thank you very much

View Tutorial          By: madonna fancy dress at 2010-05-24 20:24:27

11. thank you very much
finally i understood the usage of 'FileNameFilter' :)

View Tutorial          By: Marina at 2009-11-29 14:17:43

12. FilenameFilter only = new OnlyExt("html");
this line is showing error

View Tutorial          By: joyes at 2009-08-25 11:34:13

13. thanks for this tutorial
View Tutorial          By: d3ptzz at 2009-08-03 23:38:57

14. Thanks for posting this :)
Being a noobie @ JAVA is hard enough.. Thanks for the great explan

View Tutorial          By: C at 2008-06-23 18:17:26

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial