Home Tutorial Java Core Files How to list all drives available in the system

 
 

How to list all drives available in the system
Posted on: April 3, 2006 at 12:00 AM
In this section, you will learn how to list out all the drives available in the system.

How to list all drives available in the system

In this section, you will learn how to list out all the drives available in the system. 

Description of code:

To get the list of drives, we have called the method listRoots() of File class. This will return an array of File objects for the drives that are available in the system.

Here is the code:

import java.io.*;

class ListDrives {
  public static void main(String[] args) {
    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
      System.out.println("Drive:" + roots[i]);
    }
  }
}

Above code will print all the drives available in the system. With the help of above code you can find the drives attached to your system.

Related Tags for How to list all drives available in the system: