Posted By

hariharank12 on 12/06/10


Tagged

java files directories javaDirectoryListings javaFileListings


Versions (?)

List of files and directories inside a directory in Java


 / Published in: Java
 

  1. import java.io.File;
  2.  
  3. public class DirectoryReader {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. File folder = new File("c:/");
  8. File[] listOfFiles = folder.listFiles();
  9.  
  10. for (int i = 0; i < listOfFiles.length; i++) {
  11. if (listOfFiles[i].isFile()) {
  12. System.out.println("File " + listOfFiles[i].getName());
  13. } else if (listOfFiles[i].isDirectory()) {
  14. System.out.println("Directory " + listOfFiles[i].getName());
  15. }
  16. }
  17. }
  18.  
  19. }

Report this snippet  

You need to login to post a comment.