Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Get list of files, directories from a directory using Java program

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Core Java Example programs for Beginners and Professionals

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to generate random numbers from 0 to ... >>
<< Java program to run an application - Run Exe using...