How to List All Files in a Directory Using Java

Java


In this article, we show how to list all files in a directory using Java.

In others, we are getting all files that are in a directory.

With Java, we can get all files of any directory on a computer.

In Java, there is a listFiles() functions, which allows us to get all files in any directory that we want.

The code to create to list all files in a directory is shown below.



So in the file above we create a class called Listfiles.

In this class, we create a public static String named path and set it equal to "Documents".

We then put in our main() method.

In the main method, we create a file object, dir, and pass the parameter named "Documents" into the this File object.

So at this point in the code, the File object dir has the value of Documents.

We then use an if statement to check if this file is in fact a directory. This is done through the Java isDirectory() function. We take the File object, dir, and follow it with a dot and the isDirectory() function to check whether this object is a directory. If it is a directory, the statement returns a true value and the code advances. If it isn't a directory, this code will do nothing.

We then create an array of files called files. We this set this array of files equal to all the files in the Documents directory. This is done by invoking the listFiles() function on the dir object. Remember the dir object is set equal to the Documents directory.

We then use a foor loop in Java. This is called an enhanced for loop.

This for loop gets each file in the files array and using the Java getName() function, we are able to get the name of each file in the array.

So this is how we can retrieve all the contents of a directory using Java.

Since we didn't specify the path in the parameter we passed into File object when we created it, the Java program looks in the current directory, the one storing this Java file, to find the directory that you specified in the code.

If you want to specify a directory other than the current directory, then you will have to specify the complete path.

Since this differs according to what operating system you are using, this is the only part of Java that becomes platform dependent.

In a Windows computer, you normally specify the drive you are using with a letter such as the C drive and then specify the path from there to the directory that you want to list all the files of.

So if you have a directory you want to create that is in the C:\Users\ directory, then you would write this into the string as "C:\\Users\\"

This is because "\" is an escape character; it's used for escaping strings. To show "\" in a string, you would have to put double forward slashes "\\".



And this is all that is required to list all the files in a directory using Java.

You can also get more specific.

The above code doesn't discriminate between files and directories. It lists both files and the subdirectories in the files.

If you just want to list the files in the directories (not the subdirectories), the code would be used below.



The code above is the same as previously. The only difference is that we added the Java isFile() method. This isFile() method in Java checks to see if the f object is a file rather than a directory. If it is a file (and not a directory), then we output the name of the file using the getName() function.


Related Resources

How to Check if a File Exists Using Java

How to Check if an Object is a File Using Java

How to Check if a File is a Directory Using Java

How to Create a New File Using Java

How to Rename a File Using Java

How to Delete a File Using Java

How to Create a New Directory Using Java

How to Read a File Using Java

How to Write Data to a File Using Java

How to Get the Size of a File Using Java




HTML Comment Box is loading comments...