Jakarta Commons IO

I have written plenty of one-off no-brainer recursive Java methods that search through a directory hierarchy searching for a type of files. Such methods are good exercise problems for rookie Java programmers. But since I am a Java programmer I bearly exercise, so I tend not to write that type of method anymore. I let Jakarta Commons IO do the recusive file seach for me. Using the FileUtils, SuffixFileFilter, and TrueFileFilter from the Commons IO library I could recursively search a directory hierarchy for all groovy files with the following bit of code:

SuffixFileFilter sff = new SuffixFileFilter(".groovy");
// listFiles(File directory, IOFileFilter fileFilter, IOFileFilter  dirFilter)
Collection cl = FileUtils.listFiles(f, sff, TrueFileFilter.INSTANCE);

Other useful FileFilter classes available in the Commons IO library include PrefixFileFilter, NamedFileFilter, DirectoryFileFilter, and FalseFilterFilter.

Technorati Tags: , , , ,