Java:如何递归获取所有子目录?

在调试延迟时间外的递归函数之前:是否有一个获取子目录的命令? giveMeSubDirs(downToPath)

 // WARNING: RECURSION out of bound or too much data public HashSet getAllDirs(String path) { HashSet checkedDirs = new HashSet(); HashSet allDirs = new HashSet(); 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; } 

关于代码的摘要:

  1. 尽可能深入到目录树。 当dir中没有dir时,停止,将dir放到set中,然后向上移动。 不要检查套装中的dirs。
  2. 如果到达起始路径,请停止并返回该集。
  3. 重复步骤1和2。

PREMISE:目录结构是有限的,数据量很小。

您可以使用以下代码段获取所有子目录:

 File file = new File("path"); File[] subdirs = file.listFiles(new FileFilter() { public boolean accept(File f) { return f.isDirectory(); } }); 

这只能获得直接的子目录,以递归方式检索所有这些子句,你可以写:

 List getSubdirs(File file) { List subdirs = Arrays.asList(file.listFiles(new FileFilter() { public boolean accept(File f) { return f.isDirectory(); } })); subdirs = new ArrayList(subdirs); List deepSubdirs = new ArrayList(); for(File subdir : subdirs) { deepSubdirs.addAll(getSubdirs(subdir)); } subdirs.addAll(deepSubdirs); return subdirs; } 

不,Java标准API中没有这样的function。 但是在Apache commons-io中有 ; 如果您不想将其作为库包含,您还可以查看源代码 。

另一个版本没有递归和字母顺序。 还使用Set来避免循环(带有链接的Unix系统中的问题)。

  public static Set subdirs(File d) throws IOException { TreeSet closed = new TreeSet(new Comparator() { @Override public int compare(File f1, File f2) { return f1.toString().compareTo(f2.toString()); } }); Deque open = new ArrayDeque(); open.push(d); closed.add(d); while ( ! open.isEmpty()) { d = open.pop(); for (File f : d.listFiles()) { if (f.isDirectory() && ! closed.contains(f)) { open.push(f); closed.add(f); } } } return closed; } 

上面的示例代码缺少“);” 在声明的最后。 正确的代码应该是:

  File file = new File("path"); File[] subdirs = file.listFiles(new FileFilter() { public boolean accept(File f) { return f.isDirectory(); } }); 
 class DirFileFilter extends FileFilter { boolean accept(File pathname) { return pathname.isDirectory(); } } DirFileFilter filter = new DirFileFilter(); HashSet files = new HashSet(); void rec(File root) { // add itself to the list files.put(root); File[] subdirs = root.list(filter); // bound of recursion: must return if (subdirs.length == 0) return; else //this is the recursive case: can call itself for (File file : subdirs) rec(file); }