List All Files in a Directory in Java

In this section, we are going to learn how one can list all the files that are present in a directory. Note that a directory may contain a subdirectory, and that subdirectory may contain some files. We have to list these files, too, as these files are also part of the main directory. However, for the sake of simplicity, we will first look at the scenario where subdirectories are not present.

Directory without having Sub-directories

It means a folder is only containing files. Steps to print the files of a directory are mentioned below.

Step 1: Create a File Object for the directory.

Step 2: Obtain the array of files of that directory.

Step 3: Recursively, print the name of files (with extension) one by one.

Implementation

The following code prints the file names mentioned in the folder called Documents. The following snapshot shows the files present in the folder named Documents.

List All Files in a Directory in Java

FileName: DisplayFileExample.java

Output:

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Displaying Files from the directory: E:\Documents
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
30004399657726 report 2.pdf
30004399657726 report.pdf
5010XXXXXX8778-13 Mar 2017 TO 12 Apr 2017.pdf
a b c.txt
asp.net tutorial.pdf
coding Standards.pdf
CS121Lec01.pdf
Databases for beginners.pdf
DBMS ME CS (gate2016.info).pdf
lec1.pdf
New Text Document.txt
Programming Interview Questions.docx
Rk - Database Management Systems 3rd Edition.pdf
slip201611605845.pdf
slip201611605845_2.pdf
Task 1.pdf
The Normal Forms2.ppt

Directory with Subdirectories

It means a folder contains files as well as subfolders. Steps to print the files of a directory and its subdirectory are mentioned below.

Step 1: Create a File Object for the directory.

Step 2: Obtain the array of files and subdirectory of that directory.

Step 3: If array[j] is a file, then display the file name and recursively go to the next element of the array[j].

Step 4: If array[j] is a directory, then display then directory name, and repeat from step 2.

Implementation

We have already seen the contents of the folder named Documents. Now, we create some folders inside Documents and store number of files in it. Observe the following snapshots.

List All Files in a Directory in Java

The above snapshot shows the Documents directory has 3 sub-directories named (New folder - 1, New folder - 2, New folder - 3).

The following snapshot shows the content of the folder named New folder - 1

List All Files in a Directory in Java

The following snapshot shows the content of the folder named New folder - 2

List All Files in a Directory in Java

The following snapshot shows the content of the folder named New folder - 3

List All Files in a Directory in Java

After the creation of folders, we can write the code on the basis of the steps defined above.

FileName: DisplayFileExample1.java

Output:

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Displaying Files from the directory: E:\Documents
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
30004399657726 report 2.pdf
30004399657726 report.pdf
5010XXXXXX8778-13 Mar 2017 TO 12 Apr 2017.pdf
a b c.txt
asp.net tutorial.pdf
coding Standards.pdf
CS121Lec01.pdf
Databases for beginners.pdf
DBMS ME CS (gate2016.info).pdf
lec1.pdf
[New folder  - 1]
        How to call private method from another class in java.docx
        Java CardLayout.docx
        Java GridBagLayout.docx
        Java SpringLayout.docx
        ordinal number in java.docx
        tetrahedral number in java.docx
[New folder  - 2]
        output onlinepngtools (13).png
        output onlinepngtools (14).png
        output onlinepngtools (15).png
        output onlinepngtools (16).png
        output onlinepngtools (17).png
        output onlinepngtools (18).png
[New folder  - 3]
        Java Regular Expressions.docx
        n1 (7).png
        n2 (3).png
        n3.png
        n4.png
        String vs StringBuffer.docx
        StringBuffer in Java.docx
        StringBuffer vs StringBuilder.docx
        StringBuilder in Java.docx
New Text Document.txt
Programming Interview Questions.docx
Rk - Database Management Systems 3rd Edition.pdf
slip201611605845.pdf
slip201611605845_2.pdf
Task 1.pdf
The Normal Forms2.ppt

Next TopicTDD Java