List of files and directories inside a directory in Java


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.