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".

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Thanks for posting this :)
Being a noobie @

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

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

3. FilenameFilter only = new OnlyExt("html"
View Tutorial          By: joyes at 2009-08-25 11:34:13

4. thank you very much
finally i understood th

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

5. a very simple example that is very easy to follow
View Tutorial          By: madonna fancy dress at 2010-05-24 20:24:27

6. how to get this list to be alphabetically correct?
View Tutorial          By: Vincent at 2011-06-14 07:36:58

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

8. Hello,
this is good example, but that is on

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

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

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

11. Hello, I liked the Tutorial, but it looks exactly
View Tutorial          By: Srijan at 2013-03-15 10:15:08

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

13. while i am executing this program i am getting Nul
View Tutorial          By: Seema at 2013-11-26 18:32:52

14. while i am executing this program i am getting Nul
View Tutorial          By: Seema at 2013-11-26 18:39:15