Tuesday, April 19, 2011

How to display all the files in a directory ?

This function takes directory name and prints all files in it:

public static void displayAllDir(String dirName) {
      File dir = new File(dirName); //eg. C:
      String[] children = dir.list();
      if (children == null) {
         System.out.println( "Either dir does not exist or is 
                                 not a directory");
      }
      else {
         for (int i=0; i< children.length; i++) {
            String filename = children[i];
            System.out.println(filename);
         }
      }
   }

No comments:

Post a Comment

Chitika