Search code examples
javaapache-commonsfileutils

How to use listFilesAndDirs to get only directories without operating recursively


I'd like, for the simplicity of the code, to use FileUtils.listFilesAndDirs. I want to get all the directories in the given path, without looking at sub-directories (running recursively)

I tried this:

Collection<File> dirs = FileUtils.listFilesAndDirs(pathFile, DirectoryFileFilter.DIRECTORY, null);

but it only returned the containing directory.


Solution

  • It seems that this is not possible with FileUtils.listFilesAndDirs. If you dont need to use FileUtils.listFilesAndDirs you can do the following:

    pathFile.listFiles((java.io.FileFilter) FileFilterUtils.directoryFileFilter())