List All Files in a Directory Using Java



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
Updated on: 2020-02-20T08:21:45+05:30

960 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements