gpt4 book ai didi

java - 如何限制JAVA中递归文件列表的深度?

转载 作者:行者123 更新时间:2023-12-01 06:10:00 29 4
gpt4 key购买 nike

有没有办法限制 Java 中递归文件列表的深度?我使用 Apache commons-io 的 FileUtils.listFiles(File directory, String[] extensions, boolean recursive) 来列出指定目录的文件,但此 API 返回该目录的所有项目。

最佳答案

简单的谷歌发现了这个

/**
* Construct an instance with a directory and a file filter and an optional
* limit on the <i>depth</i> navigated to.
* <p>
* The filters control which files and directories will be navigated to as part
* of the walk. This constructor uses {@link FileFilterUtils#makeDirectoryOnly(IOFileFilter)}
* and {@link FileFilterUtils#makeFileOnly(IOFileFilter)} internally to combine the filters.
* A <code>null</code> filter means that no filtering should occur.
*
* @param directoryFilter the filter to apply to directories, null means visit all directories
* @param fileFilter the filter to apply to files, null means visit all files
* @param depthLimit controls how <i>deep</i> the hierarchy is
* navigated to (less than 0 means unlimited)
*/
protected DirectoryWalker(IOFileFilter directoryFilter, IOFileFilter fileFilter, int depthLimit) {
if (directoryFilter == null && fileFilter == null) {
this.filter = null;
} else {
directoryFilter = (directoryFilter != null ? directoryFilter : TrueFileFilter.TRUE);
fileFilter = (fileFilter != null ? fileFilter : TrueFileFilter.TRUE);
directoryFilter = FileFilterUtils.makeDirectoryOnly(directoryFilter);
fileFilter = FileFilterUtils.makeFileOnly(fileFilter);
this.filter = FileFilterUtils.or(directoryFilter, fileFilter);
}
this.depthLimit = depthLimit;
}

所以我猜你需要设置 heightLimit 变量,

引用号:http://www.programcreek.com/java-api-examples/index.php?api=org.apache.commons.io.filefilter.TrueFileFilter

关于java - 如何限制JAVA中递归文件列表的深度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686065/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com