Java 使用 FileUtils.listFiles 递归查找目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/900659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursively finding only directories with FileUtils.listFiles
提问by Dave B
I want to collect a list of allfiles under a directory, in particular including subdirectories. I like not doing things myself, so I'm using FileUtils.listFiles
from Apache Commons IO. So I have something like:
我想收集目录下所有文件的列表,特别是包括子目录。我不喜欢自己做事,所以我使用FileUtils.listFiles
Apache Commons IO。所以我有类似的东西:
import java.io.File;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
public class TestListFiles {
public static void main(String[] args) {
Collection<File> found = FileUtils.listFiles(new File("foo"),
TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File f : found) {
System.out.println("Found file: " + f);
}
}
}
Problem is, this only appears to find normal files, not directories:
问题是,这似乎只能找到普通文件,而不是目录:
$ mkdir -p foo/bar/baz; touch foo/one_file
$ java -classpath commons-io-1.4.jar:. TestListFiles
Found file: foo/one_file
I'm already passing TrueFileFilter
to both of the filters, so I can't think of anything more inclusive. I want it to list: "foo", "foo/one_file", "foo/bar", "foo/bar/baz"
(in any order).
我已经通过TrueFileFilter
了两个过滤器,所以我想不出更多的包容性。我希望它列出:("foo", "foo/one_file", "foo/bar", "foo/bar/baz"
以任何顺序)。
I would accept non-FileUtils
solutions as well, but it seems silly to have to write my own BFS, or even to collect the set of parent directories from the list I do get. (That would miss empty subdirectories anyway.) This is on Linux, FWIW.
我也接受非FileUtils
解决方案,但必须编写自己的 BFS,甚至从我得到的列表中收集父目录集似乎很愚蠢。(无论如何都会错过空的子目录。)这是在 Linux 上,FWIW。
回答by adrian.tarau
It should work, based on their API.
它应该可以工作,基于他们的 API。
Here is my own version of FileUtils, not as complete as Commons IO, it contains only what I need. Search for findFilesor you can use iterateto 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).
这是我自己的FileUtils版本,不像 Commons IO 那样完整,它只包含我需要的东西。搜索findFiles或者您可以使用iterate来避免创建庞大的列表(有时/大部分时间您只想对这些文件做一些事情,因此将它们收集在列表中是没有意义的)。
回答by Eddie
If you look at the source codeand 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 notadded to the result list.
如果您查看源代码并阅读 JavaDoc 中的两行之间的内容,您会发现——不幸的是——这个 API 并不是为了做你想做的事而设计的。它会返回一个列表文件(不列表文件和目录)符合所提供的论据。在源代码中——查看方法innerListFiles
——您将看到目录被搜索而不是添加到结果列表中。
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, allJava 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.)
我不知道有任何公共 API 可以满足您的需求。希望其他人会知道一个。大多数可能是 DFS,而不是 BFS,这对您的目的可能重要,也可能无关紧要。(到目前为止,我看过的所有Java 代码都通过深度优先搜索进行了目录树遍历。当然,这并不意味着 BFS 不存在。)
If you really want a list of everythingunder 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 Finderwill 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.
注意:Apache Commons Finder可能会支持你需要的东西,但这个库在 Commons Sandbox 中,这意味着它在这个阶段更具实验性。它可能完整,也可能不完整,可能维护也可能不维护。对于您正在寻找的内容,它也可能是重量级的。
回答by Jherico
I avoid the Java IO libraries in most of my non-trivial applications, preferring Commons VFS instead. I believe a call to this methodwith the appropriate params will accomplish your goal, but I'll grant its a long way to go for the functionality.
我在大多数非平凡应用程序中避免使用 Java IO 库,而是更喜欢 Commons VFS。我相信使用适当的参数调用此方法将实现您的目标,但我认为它的功能还有很长的路要走。
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.
其中fileObject 是FileObject 的一个实例。
回答by javamonkey79
An easier+complete Commons VFSsolution:
一个更简单+完整的Commons VFS解决方案:
FileSystemManager fsManager = VFS.getManager();
FileObject fileObject = fsManager.resolveFile( "yourFileNameHere" );
FileObject[] files = fileObject.findFiles( new FileTypeSelector( FileType.FOLDER ) )
回答by user573041
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.
希望能帮助到你。
回答by DOS
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...
只显示目录而不显示文件...