Recursively finding only directories with FileUtils.listFiles

前端 未结 6 1611
北恋
北恋 2021-01-01 16:26

I want to collect a list of all files under a directory, in particular including subdirectories. I like not doing things myself, so I\'m using FileUtils.listF

相关标签:
6条回答
  • 2021-01-01 16:40

    An old answer but this works for me:

    FileUtils.listFilesAndDirs(new File(dir), TrueFileFilter.INSTANCE, DirectoryFileFilter.DIRECTORY);
    

    shows both:

    I use:

    FileUtils.listFilesAndDirs(new File(dir), new NotFileFilter(TrueFileFilter.INSTANCE), DirectoryFileFilter.DIRECTORY)
    

    Only shows directories and not files...

    0 讨论(0)
  • 2021-01-01 16:46

    An easier+complete Commons VFS solution:

    FileSystemManager fsManager = VFS.getManager();
    FileObject fileObject = fsManager.resolveFile( "yourFileNameHere" );
    FileObject[] files = fileObject.findFiles( new FileTypeSelector( FileType.FOLDER ) )
    
    0 讨论(0)
  • 2021-01-01 16:53

    It should work, based on their API.

    Here is my own version of FileUtils, not as complete as Commons IO, it contains only what I need. Search for findFiles or you can use iterate to avoid creating huge lists(sometime/most of the time you just want to do something with those files so collecting them in a List it doesn't makes sense).

    0 讨论(0)
  • 2021-01-01 17:03

    I avoid the Java IO libraries in most of my non-trivial applications, preferring Commons VFS instead. I believe a call to this method with the appropriate params will accomplish your goal, but I'll grant its a long way to go for the functionality.

    Specifically, this 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 is an instance of FileObject.

    0 讨论(0)
  • 2021-01-01 17:03

    If you look at the source code and read between the lines in the JavaDoc, you will see that -- unfortunately -- this API is not designed to do what you want. It will return a list of files (not a list of files and directories) that match the provided arguments. In the source code -- look at the method innerListFiles -- you will see that directories are searched and not added to the result list.

    I am not aware of any public API that will do what you want. Hopefully someone else will know of one. Most will probably be a DFS, not a BFS, which may or may not matter for your purposes. (So far, all Java code I've ever looked at that did a directory tree traversal did it via a depth-first search. Which doesn't mean that BFS's aren't out there, of course.)

    If you really want a list of everything under a given directory, it's easy enough to roll your own. But I understand your wish to not reinvent the wheel.

    Note: It's possible that Apache Commons Finder will support what you need, but this library is in The Commons Sandbox, which means it is more experimental at this stage. It may or may not be complete and it may or may not be maintained. It also may be heavyweight for what you are looking for.

    0 讨论(0)
  • 2021-01-01 17:06

    Have you tried simply:

    File rootFolder = new File(...);
    File[] folders = rootFolder.listFiles((FileFilter) FileFilterUtils.directoryFileFilter());
    

    It seems to work for me. You will need recursion, of course.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题