Share on Google+Share on Google+

How to list all drives available in the system

Advertisement
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.

Advertisements

Posted on: April 3, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.