Tuesday, April 19, 2011

How to traverse a directory in java?

This can be done by getting list of all children and then traversing further deep. See following function :

public static void traverseDirectory(File dir) {
      System.out.println(dir);
      if (dir.isDirectory()) {
         String[] children = dir.list();
         for (int i = 0; i < children.length; i++) {
            traverseDirectory
            (new File(dir, children[i]));
         }
      }
   }

No comments:

Post a Comment

Chitika