Returning the Root Drives

Can you tell me how to get the root drives on a system?

The 1.0 and 1.1 version of the Java APIs did not include a means ofdetermining the root drives or partitions on a system. During thedevelopment of the Swing APIs, it became clear such a function wasneeded to be able to implement the FileChooser class.Such a feature became available with the Swing 1.1 API in thejavax.swing.filechooser.FileSystemViewclass, which had a getRoots() method that returned an array ofFile instances corresponding to the root drives orpartitions. With the release of the Java 2 Platform, thisfunctionality was moved into the File class in the form of a staticgetRoots() method. The following simple program showshow to use either method to print out the available root file systems:

import*;import javax.swing.filechooser.*;public class ShowRootDrives {  public static final void printRoots(File[] roots) {    int file;    if(roots == null) {      System.err.println("Could not determine root partitions.
");      return;    }    for(file = 0; file < roots.length; ++file)      System.out.println(roots[file].toString());  }  public static final void main(String[] args) {    FileSystemView view;    // One way is to call File.listRoots() (only in JDK 1.2)    printRoots(File.listRoots());    // Another way is to use javax.swing.filechooser.FileSystemView    // (only in Swing 1.1 or JDK 1.2)    view  = FileSystemView.getFileSystemView();    printRoots(view.getRoots());  }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: