Java:如何递归地获取所有的子目录? [英] Java: how to get all subdirs recursively?
本文介绍了Java:如何递归地获取所有的子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在调试晚到小时的绑定递归函数之前:是否有命令获取子目录? giveMeSubDirs(downToPath)
?
//警告:RECURSION出界或太多的数据
public HashSet< FileObject> getAllDirs(String path){
HashSet< FileObject> checkedDirs = new HashSet< FileObject>();
HashSet< FileObject> allDirs = new HashSet< FileObject>();
字符串startingPath =路径;
文件fileThing = new File(path);
FileObject fileObject = new FileObject(fileThing); $!
$ b $ for(FileObject dir:getDirsInDir(path)){
// SUBDIR
while(!checkedDirs.contains(dir)$ b $ (&); getDirsInDir(dir.getFile()。getParent())。size()== 0)){
//不检查顶部DIRS,如果有任何底部的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;
//接近根目录
path = traverseDownOneLevel(path);
}
path = giveAnUncheckedDir(path,checkedDirs);
if(path ==NoUnchecked。){
checkedDirs.add(new FileObject((new File(path))。getParentFile()));
break;
}
}
}
}
return allDirs;
关于代码的总结
- 尽可能深入目录树。当目录中没有目录时,停止,将目录放到目录中,向上移动。不要在集合中检查dirs。
-
-
- 重复第1步和第2步
- 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.
ol>
PREMISE:目录结构是有限的,而且数据量小。
解决方案
File file = new File(path);
File [] subdirs = file.listFiles(new FileFilter(){
public boolean accept(File f){
return f.isDirectory();
}
});
这只会得到直接的子代码,以递归的方式检索所有的子代码:
列表< File> getSubdirs(文件文件){
列表< File> (File f){
return f.isDirectory();
}
})) ;
subdirs = new ArrayList< File>(subdirs);
列表< File> deepSubdirs = new ArrayList< File>();
(File subdir:subdirs){
deepSubdirs.addAll(getSubdirs(subdir));
}
subdirs.addAll(deepSubdirs);
返回子目录;
}
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:
PREMISE: the directory-structure is finite and with a small data amount.
解决方案
You can get all subdirs with the following snippet:
File file = new File("path");
File[] subdirs = file.listFiles(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory();
}
});
This gets only immediate subdirs, to retrieve all of them recursively you could write:
List<File> getSubdirs(File file) {
List<File> subdirs = Arrays.asList(file.listFiles(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory();
}
}));
subdirs = new ArrayList<File>(subdirs);
List<File> deepSubdirs = new ArrayList<File>();
for(File subdir : subdirs) {
deepSubdirs.addAll(getSubdirs(subdir));
}
subdirs.addAll(deepSubdirs);
return subdirs;
}
这篇关于Java:如何递归地获取所有的子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文