Java:如何递归获取所有子目录?
在调试 Late-hour-out-of-bound-recursive-function 之前:是否有获取子目录的命令? giveMeSubDirs(downToPath)
?
// WARNING: RECURSION out of bound or too much data
public HashSet<FileObject> getAllDirs(String path) {
HashSet<FileObject> checkedDirs = new HashSet<FileObject>();
HashSet<FileObject> allDirs = new HashSet<FileObject>();
String startingPath = path;
File fileThing = new File(path);
FileObject fileObject = new FileObject(fileThing);
for (FileObject dir : getDirsInDir(path)) {
// SUBDIR
while ( !checkedDirs.contains(dir)
&& !(getDirsInDir(dir.getFile().getParent()).size() == 0)) {
// DO NOT CHECK TOP DIRS if any bottom dir UNCHECKED!
while ( uncheckedDirsOnLevel(path, checkedDirs).size() > 0) {
while (getDirsInDir(path).size() == 0
|| (numberOfCheckedDirsOnLevel(path, checkedDirs)==getDirsInDir(path).size())) {
allDirs.add(new FileObject(new File(path)));
checkedDirs.add(new FileObject(new File(path)));
if(traverseDownOneLevel(path) == startingPath )
return allDirs;
//get nearer to the root
path = traverseDownOneLevel(path);
}
path = giveAnUncheckedDir(path, checkedDirs);
if ( path == "NoUnchecked.") {
checkedDirs.add(new FileObject( (new File(path)).getParentFile() ));
break;
}
}
}
}
return allDirs;
}
代码摘要:
- 尽可能深入到目录树。当dir中没有dir时,停止,将dir放入集合中,向上遍历。不要检查集合中的目录。
- 如果到达起始路径,请停止并返回集合。
- 重复步骤1和2。
前提:目录结构有限且数据量较小。
Before debugging the late-hour-out-of-bound-recursive-function: is there a command to get subdirs? giveMeSubDirs(downToPath)
?
// WARNING: RECURSION out of bound or too much data
public HashSet<FileObject> getAllDirs(String path) {
HashSet<FileObject> checkedDirs = new HashSet<FileObject>();
HashSet<FileObject> allDirs = new HashSet<FileObject>();
String startingPath = path;
File fileThing = new File(path);
FileObject fileObject = new FileObject(fileThing);
for (FileObject dir : getDirsInDir(path)) {
// SUBDIR
while ( !checkedDirs.contains(dir)
&& !(getDirsInDir(dir.getFile().getParent()).size() == 0)) {
// DO NOT CHECK TOP DIRS if any bottom dir UNCHECKED!
while ( uncheckedDirsOnLevel(path, checkedDirs).size() > 0) {
while (getDirsInDir(path).size() == 0
|| (numberOfCheckedDirsOnLevel(path, checkedDirs)==getDirsInDir(path).size())) {
allDirs.add(new FileObject(new File(path)));
checkedDirs.add(new FileObject(new File(path)));
if(traverseDownOneLevel(path) == startingPath )
return allDirs;
//get nearer to the root
path = traverseDownOneLevel(path);
}
path = giveAnUncheckedDir(path, checkedDirs);
if ( path == "NoUnchecked.") {
checkedDirs.add(new FileObject( (new File(path)).getParentFile() ));
break;
}
}
}
}
return allDirs;
}
Summary about the code:
- Go as deep to the directory tree as possible. When there is no dir in a dir, stop, put the dir to the set, traverse up. Do not check dirs in the set.
- Stop and return the set if you reach the starting path.
- Repeat steps 1 and 2.
PREMISE: the directory-structure is finite and with a small data amount.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用以下代码片段获取所有子目录:
这仅获取直接子目录,要递归地检索所有子目录,您可以编写:
You can get all subdirs with the following snippet:
This gets only immediate subdirs, to retrieve all of them recursively you could write:
不,Java 标准 API 中没有这样的功能。但有 Apache commons-io;如果您不想将其包含为库,您也可以 看源码。
No, there is no such functionality in the Java standard API. But there is in Apache commons-io; if you don't want to include it as a library, you could also look at the source code.
另一个版本没有递归和字母顺序。还使用 Set 来避免循环(Unix 系统中存在链接的问题)。
Another version with no recursion, and alphabetical order. Also uses a Set to avoid loops (a problem in Unix systems with links).
上面的示例代码缺少“);”在声明的末尾。
正确的代码应该是:
The sample code above is missing ");" at the end of the statement.
The correct code should be:
使用递归:
Using recursion:
这是使用 Java 8 方法改进的代码。该代码将在递归的基础上运行并查找目录直到最后一个根目录。
如果您只想要直接子目录列表,请尝试使用以下代码行。
This is an improved code with Java 8 approach. This code will run on a Recursion basis and find the directories until the last root.
If you want only immediate subdirectories list, try with the below line of code..
所有放入 lambda 魔法的内容:
只需从根文件开始(应该是一个目录)和一个空列表。
注意: 步骤 1 & 2 可以与过滤器结合使用(@see listFiles(FileFilter 过滤器))
All that put into some lambda magic:
Just start with root file (which should be a directory) and an empty list.
Note: Step 1 & 2 can be combined with a filter (@see listFiles(FileFilter filter))