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.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip