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

如何限制JAVA中递归文件列表的深度?,java,file-listing,Java,File Listing,有没有办法限制Java中递归文件列表的深度? 我正在使用FileUtils.ListFileFile目录、String[]扩展名、ApacheCommons io的布尔递归来列出指定目录的文件,但此API返回此目录的所有项。一个简单的Google发现了这一点 /** * Construct an instance with a directory and a file filter and an optional * limit on the <i>depth</i>

有没有办法限制Java中递归文件列表的深度?
我正在使用FileUtils.ListFileFile目录、String[]扩展名、ApacheCommons io的布尔递归来列出指定目录的文件,但此API返回此目录的所有项。

一个简单的Google发现了这一点

/**
 * 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;
}
所以我想你需要设置depthLimit变量

参考:

使用