Java Program to Get List of Files, Directories From a Directory

You are currently viewing Java Program to Get List of Files, Directories From a Directory

Java program to get list of files, directories from a directory

import java.io.File;

class GetFilesFromDirectory {
  public static void main(String[] args) {
    File dirName = new File("D:/eclipse");
    File[] listFiles = dirName.listFiles();

    for (int i = 0; i < listFiles.length; i++) {
      if (listFiles[i].isFile()) {
        System.out.println("File: " + listFiles[i].getName());
      } else if (listFiles[i].isDirectory()) {
        System.out.println("Directory: " + listFiles[i].getName());
      }
    }
  }
}

Output

    File: .eclipseproduct
    File: artifacts.xml
    Directory: configuration
    Directory: dropins
    File: eclipse.exe
    File: eclipse.ini
    File: eclipsec.exe
    File: epl-v10.html
    Directory: features
    File: notice.html
    Directory: p2
    Directory: plugins
    Directory: readme

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply