Traverse only directories under dir in Java
Description
The following code shows how to traverse only directories under dir.
Example
/*from w ww . j a va 2 s . c o m*/
import java.io.File;
public class Main {
public static void main(String[] argv) throws Exception {
visitAllDirs(new File("c:/"));
}
// Process only directories under dir
public static void visitAllDirs(File dir) {
if (dir.isDirectory()) {
System.out.println(dir);
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
visitAllDirs(new File(dir, children[i]));
}
}
}
}
The code above generates the following result.