Wednesday 11 February 2015

Read All Files From Any Directory(Folder)

Read a given directory(Folder). Filer its file and folder(directory).
and also show name of files and name of folder

import java.io.File;

public class ReadDirectory {
public static void main(String[] args) {
     String path="D:\\a";      
        try {
            File folder= new File(path);
            File[] listOfFiles= folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
          if (listOfFiles[i].isFile()) {
                  System.out.println("File=" + listOfFiles[i].getName());        
          } else if (listOfFiles[i].isDirectory()) {
        System.out.println("Directory=" + listOfFiles[i].getName());
          }
          }
        } catch (Exception e) {
            System.out.println(e);
        }

}
}

No comments:

Post a Comment