[ACCEPTED]-Recursively finding only directories with FileUtils.listFiles-apache-commons-io
An old answer but this works for me:
FileUtils.listFilesAndDirs(new File(dir), TrueFileFilter.INSTANCE, DirectoryFileFilter.DIRECTORY);
shows 2 both:
I use:
FileUtils.listFilesAndDirs(new File(dir), new NotFileFilter(TrueFileFilter.INSTANCE), DirectoryFileFilter.DIRECTORY)
Only shows directories and not 1 files...
Have you tried simply:
File rootFolder = new File(...);
File[] folders = rootFolder.listFiles((FileFilter) FileFilterUtils.directoryFileFilter());
It seems to work for 2 me. You will need recursion, of course.
Hope 1 it helps.
If you look at the source code and read between the 25 lines in the JavaDoc, you will see that 24 -- unfortunately -- this API is not designed 23 to do what you want. It will return a list 22 of files (not a list of files and directories) that match the provided 21 arguments. In the source code -- look at 20 the method innerListFiles
-- you will see that directories 19 are searched and not added to the result list.
I 18 am not aware of any public API that will 17 do what you want. Hopefully someone else 16 will know of one. Most will probably be 15 a DFS, not a BFS, which may or may not matter 14 for your purposes. (So far, all Java code 13 I've ever looked at that did a directory 12 tree traversal did it via a depth-first 11 search. Which doesn't mean that BFS's aren't 10 out there, of course.)
If you really want 9 a list of everything under a given directory, it's 8 easy enough to roll your own. But I understand 7 your wish to not reinvent the wheel.
Note: It's 6 possible that Apache Commons Finder will support what you need, but 5 this library is in The Commons Sandbox, which 4 means it is more experimental at this stage. It 3 may or may not be complete and it may or 2 may not be maintained. It also may be heavyweight 1 for what you are looking for.
I avoid the Java IO libraries in most of 7 my non-trivial applications, preferring 6 Commons VFS instead. I believe a call to 5 this method with the appropriate params will accomplish 4 your goal, but I'll grant its a long way 3 to go for the functionality.
Specifically, this 2 code will do what you want:
FileObject[] files = fileObject.findFiles(new FileSelector() {
public boolean includeFile(FileSelectInfo fileInfo) {
return fileInfo.getFile().getType() == FileType.FOLDER; }
public boolean traverseDescendents(FileSelectInfo fileInfo) {
return true;
}
});
where fileObject 1 is an instance of FileObject.
An easier+complete Commons VFS solution:
FileSystemManager fsManager = VFS.getManager();
FileObject fileObject = fsManager.resolveFile( "yourFileNameHere" );
FileObject[] files = fileObject.findFiles( new FileTypeSelector( FileType.FOLDER ) )
0
It should work, based on their API.
Here 7 is my own version of FileUtils, not as complete as 6 Commons IO, it contains only what I need. Search 5 for findFiles or you can use iterate to avoid creating huge 4 lists(sometime/most of the time you just 3 want to do something with those files so 2 collecting them in a List it doesn't makes 1 sense).
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.