Tuesday, 4 August 2015

Getting list of files/subdirectories in a directory using Java

public String[] filesInDir(String dirName){
  String[] files = null; 
  try {
    File dir = new File(dirName);
    if (dir.isDirectory()) {
      files = dir.list();
    }
    else if (!dir.exists()){
      System.out.println("Directory doesn't exist");
      System.exit(0);
   }
   } catch (Exception e) {
     e.printStackTrace();
   } 
  return files;
}

No comments:

Post a Comment