I am trying to figure out how to use apache commons io DirectoryWalker. It's pretty easy to understand how to subclass DirectoryWalker. But how do you start executing it on a particular directory?
+3
A:
It looks like the subclass should provide a public method that calls walk().
Duck
2009-07-07 00:49:55
argh!!! of course, how the @#$@#$@# did I miss seeing that?
Jason S
2009-07-07 00:51:17
don't you hate it when that happens >.<
Nippysaurus
2009-07-07 00:53:51
actually it looks like you don't have to provide a method that calls walk. another class can call walk() from the outside.
Jason S
2009-07-07 00:58:58
d'oh! I stand corrected, walk() is protected. you are 100% right!
Jason S
2009-07-07 02:13:30
+4
A:
Just to expand on this answer since I was puzzled at first of how to use the class as well and this question came up on google when I was looking around. This is just an example of how I used it (minus some things):
public class FindConfigFilesDirectoryWalker extends DirectoryWalker {
private static String rootFolder = "/xml_files";
private Logger log = Logger.getLogger(getClass());
private static IOFileFilter filter = FileFilterUtils.andFileFilter(FileFilterUtils.fileFileFilter(),
FileFilterUtils.suffixFileFilter("xml"));
public FeedFileDirectoryWalker() {
super(filter, -1);
}
@SuppressWarnings("unchecked")
@Override
protected void handleFile(final File file, final int depth, final Collection results) throws IOException {
log.debug("Found file: " + file.getAbsolutePath());
results.add(file);
}
public List<File> getFiles() {
List<File> files = new ArrayList<File>();
URL url = getClass().getResource(rootFolder);
if (url == null) {
log.warn("Unable to find root folder of configuration files!");
return files;
}
File directory = new File(url.getFile());
try {
walk(directory, files);
}
catch (IOException e) {
log.error("Problem finding configuration files!", e);
}
return files;
}
}
And then you would just invoke it via the public method you created, passing in any arguments that you may want:
List<File> files = new FindConfigFilesDirectoryWalker().getFiles();
rcl
2009-08-18 15:23:37