How to list all 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.

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.

Ads