Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Jul 14, 1999

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 of determining the root drives or partitions on a system. During the development of the Swing APIs, it became clear such a function was needed to be able to implement the FileChooser class. Such a feature became available with the Swing 1.1 API in the javax.swing.filechooser.FileSystemViewclass, which had a getRoots() method that returned an array of File instances corresponding to the root drives or partitions. With the release of the Java 2 Platform, this functionality was moved into the File class in the form of a static getRoots() method. The following simple program shows how 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.\n");
      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());
  }
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date