Java Examples - Display root directories

Advertisements


Problem Description:

How to display root directories in the system?

Solution:

Following example shows how to find root directories in your system using listRoots() method of File class.

import java.io.*;

class Main{
   public static void main(String[] args){
      File[] roots = File.listRoots();
      System.out.println("Root directories 
      in your system are:");
      for (int i=0; i < roots.length; i++) {
         System.out.println(roots[i].toString());
      }
   }
}

Result:

The above code sample will produce the following result. The result may vary depending on the system & operating system. If you are using Unix system you will get a single root '/'.

Root directories in your system are:
C:\
D:\
E:\
F:\
G:\
H:\


Advertisements
Advertisements