How to List all Files in a Directory in Java? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Java provides a feature to interact with the file system through the java.io.file package. This feature helps developers automate the things to operate with files and directories. In this article, we will learn how to list all the files in a directory in Java. Approaches to list all files in a directory We have two ways to get the list of all the files in a directory in Java. Using listFiles()Using Java New I/O PackageProgram to list all files in a directory in JavaMethod 1: Using listFiles()In the java.io.File class which provides an inbuilt method listFiles() that will return an array of File objects of the files in the directory. Java // Java program list all files in a directory using listFiles() import java.io.File; public class ListFilesExample { public static void main(String[] args) { // Path of the specific directory String directoryPath = "C:/Users/GFG0354/Documents/JavaCode"; // Using File class create an object for specific directory File directory = new File(directoryPath); // Using listFiles method we get all the files of a directory // return type of listFiles is array File[] files = directory.listFiles(); // Print name of the all files present in that path if (files != null) { for (File file : files) { System.out.println(file.getName()); } } } } Output: Explanation of the Program:In the above program, we have specified the path of the directory in the directoryPath variable.We have created a File object named directory representing the specified directory path.We have used the listFiles() method of the File class to retrieve an array of File objects representing the files and directories contained within the specified directory.We iterate over the array of File objects and print the name of each file using the getName() method.Method 2: Using Java New I/O PackageNew I/O provides a new easy way to interact with Files and apply file operations. The Files and Path classes are available in the java.nio.file package which we can use to list the files in a directory. Java // Java program to list all files in a directory using I/O Package import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.FileVisitOption; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.util.EnumSet; public class NewIOExample { public static void main(String[] args) throws IOException { // path of the specific directory String directoryPath = "C:/Users/GFG0354/Documents/JavaCode"; // create a Path object for the specified directory Path directory = Paths.get(directoryPath); // use DirectoryStream to list files which are present in specific try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) { //with forEach loop get all the path of files present in directory for (Path file : stream) { System.out.println(file.getFileName()); } } } } Output: Explanation of the Program:In the above program, we have specified the path of the directory in the directoryPath variable.We have created a Path object named directory representing the specified directory path using the Paths.get() method.We have used the Files.newDirectoryStream() method to create a DirectoryStream for the specified directory. Comment More infoAdvertise with us Next Article How to List all Files in a Directory in Java? N nandinigujral Follow Improve Article Tags : Java Java Programs Java Examples Practice Tags : Java Similar Reads List all Files from a Directory Recursively in Java In Java, when working with file systems, sometimes it is necessary to list all files from a directory, including those within its subdirectories. This process, known as recursive directory traversal, allows us to explore the entire hierarchy of files and folders starting from a given directory. Java 3 min read How to Create a Directory in Java? In Java, creating a directory is a common operation when working with file system manipulation. Directories are used to organize files and other directories into a hierarchical structure. This article will guide you through the process of creating a directory in Java, providing step-by-step examples 2 min read Java Program to Search for a File in a Directory Searching files in Java can be performed using the File class and FilenameFilter interface. The FilenameFilter interface is used to filter files from the list of files. This interface has a method boolean accept(File dir, String name) that is implemented to find the desired files from the list retur 3 min read How to Get the File's Parent Directory in Java? Java is one of the most popular and powerful programming languages. It is widely used for various purposes. In this article, we are going to learn how to find the parent directory of a file or a specified path. Methods to Get the Parent Directory of a fileIn Java, we can get the parent Directory wit 3 min read How to Watch a Directory for Changes in Java ? In this article, we will learn how to watch the directory changes through the Java programming language. Java provides a robust and efficient way to monitor directories for changes through the java.nio.file package. The WatchService API allows developers to receive notifications about various events 2 min read Like