Listing Files in a Directory

If you are developing an application it is often required to list the files in a directory . The following code shows the way in which files can be listed in Java.


public class ListFiles {

public static void main(String[] args) {
File file = new File("E:/temp");
if (file.isDirectory()) {
for (File childFile : file.listFiles()) {
System.out.println("File Name :" + childFile.getName());
} else {
System.out.println("The path is not a directory");

Leave a Reply

Your email address will not be published. Required fields are marked *