How to list all files in a directory using Java?

Java 8Object Oriented ProgrammingProgramming

You can get the list of files in a directory −

  • Create a directory object using the File class.
  • Get the list of directories in it using the getName() method.

Example

import java.io.File;

public class FindingDirectories {
   public static void main(String args[]) {
      String dir ="C:/Users/Tutorialspoint/Desktop/movies";
      File directory = new File(dir);
      File[] fileList = directory.listFiles();

      for(File file: fileList) {
         System.out.println(file.getName());
      }
   }
}

Output

Arundhati HD.mp4
Okka Ammai Tappa.mkv
Padamati Sandhya Ragam.mp4
raja
Published on 12-Jan-2018 06:09:12
Advertisements