FilenameFilter - sample program in Java

By: Reema sen Emailed: 1767 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.

import java.io.*;
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.
import java.io.*;
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++) {
System.out.println(s[i]);
}
}
}

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


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(9)


1. View Comment

Thanks for posting this :)
Being a noobie @ JAVA is hard enough.. Thanks for the great explanation and code example.


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

thanks for this tutorial

View Tutorial          By: d3ptzz at 2009-08-03 23:38:57
3. View Comment

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


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

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


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

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
6. View Comment

Lots of thanks for this example

View Tutorial          By: Santosh at 2011-06-28 16:42:25
7. View Comment

Hello,
this is good example, but that is only for one extension, how about multiple extensions at the same time like .txt, .xml, .rtf
etc..
anyone can help on this please
Thank you
Zubair


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

you can put the a loop for multiple extension

View Tutorial          By: Arun at 2012-12-07 05:23:47
9. View Comment

while i am executing this program i am getting NullPointerException by using throws i added this exception but again and again i am getting same message please help me

View Tutorial          By: Seema at 2013-11-26 18:39:15

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Reema sen
this keyword sample in Java
Using the DriverManager Class vs Using a DataSource Object for a connection
Steps to get a Free SSL certificate for your Tomcat
fgets(), fputs() - Line Input and Output - sample program in C
Address Arithmetic and pointers in C
Constants and escape sequences in C
Implementing Pure Virtual Functions in C++
strcat() and strncat() sample program in C++
paint() sample program to draw a line in J2ME
Creating and Handling JAR files
SequenceInputStream example program in Java
FilenameFilter - sample program in Java
Transient vs Volatile modifiers in Java
while (1) Loops in C++
Use of 'finally' in Java

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
A Serialization Example in Java
Type Casting in Java
instanceof sample program in Java
Using One-Dimensional Arrays in Java
Method Overriding in Java
indexOf( ) and lastIndexOf( ) in Java
The java Buzzwords
Write to a file in Java - Sample Program
XML and Java - Parsing XML using Java Tutorial
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Java program to check if user input is an even number
HTTPS Java - URL Connection program. Using HttpsURLConnection
Most Emailed Articles (in Java)
Converting a number into its equalant value in words in Java
java.lang.reflect package
Disadvantages of using Native methods in Java
What is UCS? What is ISO 10646?
Generating Your Key Pair example using keytool in Java
Operator Precedence in Java
PushbackReader sample program in Java
concat(), replace(), and trim() Strings in Java
instanceof sample program in Java
Use of - new - in Java
The Benefits of OOP
Increment and Decrement Operator
Characters in java
The continuing Revolution of java
Why java is important to the Internet