Get Base Root Directories of a System in Java


The base root directories, root folder or root refers to the base or main drives of a system. They have the highest hierarchy and are the start or beginning of a particular folder structure. The root directory contains all other folders and files in it.

As per the problem statement, we have to get the base directories of a system. Number of directories depends on the system as it varies system to system. Multiple drive/directories can be created by partitioning a single drive/directory.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Suppose we have 5 directories in our system.

Then the program should list the drives.

Ex-
Drive C
Drive D
Drive E
Drive F
Drive G

Instance-2

Suppose we have only 1 directory in our system.

Then the program should list the drives.

Ex-
Drive C

Algorithm

  • Step 1 − Import java.io.File package

  • Step 2 − Create an array of File objects and store the root directories using listRoots() function.

  • Step 3 − Use a for loop to iterate all the elements of the array and print them.

Syntax

To get the root directories we need to call a function of the file class called listRoots().

Following is the syntax of the method −

File[] object_name = File.listRoots()

To get details like root partitions, file type information, or hidden file bits which are not generally accessible through the File API. To use its methods we need to create a class object.

Following is the syntax to create an object of this class −

FileSystemView fsv = FileSystemView.getFileSystemView();

Multiple Approaches

We have provided the solution in different approaches

  • Print All Root Directories

  • Print All Root Directories with Details.

Let’s see the program along with its output one by one.

Approach-1: Print All Root Directories

In this approach, we use the listRoots() method of the File class and print it to get the base root directories in our system.

Example

import java.io.File;
public class Main {
   public static void main(String args[]) {
      
      // Created an array of file objects to store the root directories
      File[] rootDrive = File.listRoots();
      
      // Use a for loop to print out the array elements
      for (File sysDrive : rootDrive) {
         System.out.println("Drive : " + sysDrive);
      }
   }
}

Output

Drive : /

Approach-2: Print All Root Directories with Details

In this approach, we use the listRoots() method of the File class like the previous approach with the FileSystemView class to print the directories with their type, space free and total space.

Example

import java.io.File;
import javax.swing.filechooser.FileSystemView;
public class Main {
   
   // Constant that stores 1 gigabyte in bytes 
   static long gbConvert = 1073741824l;
   public static void main(String args[]) {
   
      // Created a filesystemview object to store 
      FileSystemView fsv = FileSystemView.getFileSystemView();
      
      // Created an array of file objects to store the root directories 
      File[] rootDrive = File.listRoots();
      
      // Use a for loop to print out the array elements 
      for (File sysDrive : rootDrive) {
      
         // Print drive letter
         System.out.println("Drive : " + sysDrive);
         
         // Print the disk type
         System.out.println("Type: " +
         fsv.getSystemTypeDescription(sysDrive));
         
         // Print the space occupied and total space
         System.out.println("Space occupied(in GB): " + (sysDrive.getTotalSpace() - sysDrive.getFreeSpace()) / gbConvert + "/"
         + sysDrive.getTotalSpace() / gbConvert);
         System.out.println();
      }
   }
}

Output

Drive : /
Type: null
Space occupied(in GB): 122/874

In this article, we explored different approaches to get the base directories along with its details by using Java programming language.

Updated on: 02-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements