Skip to content
Related Articles

Related Articles

Improve Article
Java Program to Display all the Directories in a Directory
  • Last Updated : 05 Nov, 2020

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



  1. Terminal Command used to compile any java code on the machine
  2. Terminal Command used to Run any java code on the machine
javac class_name.java // For Compilation
java class_name       // For Execution     
ActionCommand
To navigate into the root directorycd /
To navigate to your home directorycd
To navigate up one directory levelcd ..
To navigate to the previous directorycd –
To lookup for files inside the current instance directoryls

Approach: 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);
                }
  
              // Simpley 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 windos user write path in below formate-:
              ("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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :