Recursively finding only directories with FileUtils.listFiles

前端 未结 6 1647
北恋
北恋 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 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.

提交回复
热议问题