Skip to content

Java FilenameFilter example – How to search a directory using java

November 23, 2011

Hi folks,

Following program filters the all the .xml files inside of the “d:/test_folder” directory. Please note that I have provided an inline implementation for the FileNameFilter, which acts as the searching criteria.

File dirFile = new File(“D:/test_folder”);
File[] files = dirFile.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(“.xml”);
}
});

//iterating the sub xml files list
for (File localFile : files) {
//what ever you want to do

}

Hope this helps.

Thank you.

Regards,

Lasith.

Advertisement

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.