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.

import java.io.*;

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");
}
System.out.println("Finished");
}
}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">