Display list of files in the given directory

25 Feb

Hi folks,

Yesterday one of my colleague was aksing, how to get list of available .txt files from the given directory. Here is the post with code to fetch the list of files from the given path (list of files in the folder).

public class ListOfFilesDemo
public static void main(String[] args) {
// Directory path here
String path = “.”;
String fileName;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++){
if (listOfFiles[i].isFile()) {
fileName = listOfFiles[i].getName(); // Line1
System.out.println(“Filename :: “+fileName);

If you want to list only .txt files, put the below code after Line1 in the above program:

if (fileName!= null && (fileName.endsWith(“.txt”) || fileName.toUpperCase().endsWith(“.TXT”))){

Happy Coding 🙂

Get glued to know more updates.

Have a nice day.

Leave a comment

Posted by on February 25, 2013 in Uncategorized


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: