I am trying to recursively search the directory and list all .txt files found. This is my code for it:
private static void listFilesForFolder(File folder) throws FileNotFoundException {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(Arrays.toString(fileEntry.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".txt");
}
})));
}
}
}
I'm using FileFilter to print out all the .txt files but it prints out null instead. Anyone know why that's the case?
I think instead of using a
FileFilterin theelseblock, you can simply use anifstatement. Because in thiselseblock you always have a file (not a directory). See whether below change works for you.EDIT:
If you really want to do this using
FileFilter, you can do it like this: