devxlogo

Returning the Root Drives

Returning the Root Drives

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

Answer:
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 java.io.*;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 

devx-admin

Share the Post: