Java Program to Display all the Directories in a Directory
Last Updated :
22 Jun, 2024
The directory is the organizing structure of the computer file system which is not only responsible for storing files but also to locate them on the memory. File organization structure is a hierarchical structure involving parent and child relationships just like what is there in tree data structures. It will vary from operating systems, rather the efficiency of the operating system is measured with the ease of file organization structure for the end-users.
The following classes in Java are present there to list directory content
- java.nio.file.Files
- org.apache.commons.io.FileUtils
- java.io.File
The hierarchical structure of Directory

Prerequisite: File And Directory Commands
- Terminal Command used to compile any java code on the machine
- Terminal Command used to Run any java code on the machine
java class_name.java // For Compilation
java class_name // For Execution
Action | Command |
---|
To navigate into the root directory | cd / |
To navigate to your home directory | cd |
To navigate up one directory level | cd .. |
To navigate to the previous directory | cd – |
To lookup for files inside the current instance directory | ls |
Approaches Display all the Directories in a Director
There are two standard approaches in accessing the directory in Java. Discussion over approaches is given below:
Approach 1:
listFiles() method to store all files in an array to print all the files present in the current folder.
Here is the implementation for the same.
Java
// Java Program to Display all directories in a directory
// Importing Classes/Files
import java.io.*;
// Importing specific File Class for directory access
import java.io.File;
class GFG {
// Main Driver code
public static void main(String[] args)
{
// Creating object of class File where
// Dot represents the current directory
File currentDir = new File(".");
displayDirectory(currentDir);
}
// Function displaying all the directories
// present in the directory
public static void displayDirectory(File dir)
{
try {
File[] files = dir.listFiles();
// For-each loop for iteration
for (File file : files) {
// Checking of file inside directory
if (file.isDirectory()) {
// Display directories inside directory
System.out.println(
"directory:"
+ file.getCanonicalPath());
displayDirectory(file);
}
// Simply get the path
else {
System.out.println(
" file:"
+ file.getCanonicalPath());
}
}
}
// if any exceptions occurs printStackTrace
catch (IOException e) {
e.printStackTrace();
}
}
}
Output: Terminal displaying all the directories in a directory

Approach 2:
Now, here simply the Brute-force attack is taken into consideration that is to access the given directory and simply printing the directories/file present in the specified folder. Here is the implementation for the same.
Java
// Java Program to Display all directories in a directory
// Importing Classes/Files
import java.io.*;
public class GFG {
// Driver main method
public static void main(String[] args)
{
/*For windows user write path in below format-:
("F:\\folder name \\ subfolder")
*/
// Creating new instance of File
File file = new File("/Desktop");
// returns an array of all files
String[] fileList = file.list();
for (String str : fileList) {
System.out.println(str);
}
}
}
Output:
This code accesses over network volume.

Note: In the above example we have written the path for Linux user. For Windows users, you have to use double-backslash while specifying the path. It is because the \ character is used as an escape character in Java. Hence, the first backslash is used as an escape character for the second one.
Conclusion
In conclusion, this blog post has demonstrated how to effectively use Java to display all directories within a specified directory. By utilizing Java’s file handling capabilities and recursion, we’ve created a program that navigates through directories, identifies subdirectories, and outputs their paths to the console.
Similar Reads
Java program to merge contents of all the files in a directory
Prerequisite : PrintWriter, BufferedReader. We are given a directory/folder in which n number of files are stored(We dont know the number of files) and we want to merge the contents of all the files into a single file lets say output.txt For the below example lets say the folder is stored at the pat
2 min read
Java Program to Create a File in a Specified Directory
Creating a file in a specific directory using Java can be done in various ways. This is done using predefined packages and Classes. In this article, we will learn about how to create a file in a specified directory. Methods to Create a File in a Specified DirectoryThree such methods to create a file
3 min read
Java Program to Search for a File in a Directory
Searching files in Java can be performed using the File class and FilenameFilter interface. The FilenameFilter interface is used to filter files from the list of files. This interface has a method boolean accept(File dir, String name) that is implemented to find the desired files from the list retur
3 min read
Java Program to Delete a directory
The class named java.io.File represents a file or directory (path names) in the system. This class provides methods to perform various operations on files/directories. The delete() method of the File class deletes the files and empty directory represented by the current File object. If a directory i
2 min read
How to List all Files in a Directory in Java?
Java provides a feature to interact with the file system through the java.io.file package. This feature helps developers automate the things to operate with files and directories. In this article, we will learn how to list all the files in a directory in Java. Approaches to list all files in a direc
3 min read
Java Program to Calculate and Display Area of a Circle
Given a radius of the circle, write a java program to calculate and display the area of the circle. (Take â=3.142) Example Input : radius= 5 Output: Area of circle is : 78.55 Input : radius= 8 Output: Area of circle is : 201.08As we know to calculate the area of a circle, the radius of the circle mu
1 min read
Java Program to Get the Size of a Directory
The size of files in Java can be obtained using the File class. The in-built function of 'fileName.length()' is used to find size of file in Bytes. The directory may contain 'N' number of files, for calculating the size of the directory summation of the size of all the files is required. length() Me
2 min read
Java Program to Traverse in a Directory
A directory is an organizational file system structure that contains Files and Directorates. Even an attacker can try to traverse or access a folder which we name as 'File Traversal Attack or Path Traversal Attack a different directory. In short here the directory is traversed which is outside the h
3 min read
Java Program to Check if a Directory is Empty or Not
The list() method defined in class java.io.File which is used to obtain the list of files and folders(directories) present in the specified directory defined by its pathname. The list of files is stored in an array of string. If the length of an array is greater than 0, then the specified directory
3 min read
Java Program to Get the Basic File Attributes
Basic File Attributes are the attributes that are associated with a file in a file system, these attributes are common to many file systems. In order to get the basic file attributes, we have to use the BasicFileAttributes interface. This interface is introduced in 2007 and is a part of the nio pack
3 min read