Java FilenameFilter example – How to search a directory using java
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
Leave a Comment