This example shows how to list the system drive letters using Java program. File.listRoots() method returns the drive letter from the file system. Lets look at the simple example for listing the operating system’s drive letters for windows.
ListOSDrivesExample.java
package javabeat.net.io; import java.io.File; /** * Find all drive letters in Java Example * * @author Krishna * */ public class ListOSDrivesExample { public static void main(String[] args) { File[] roots = File.listRoots(); for(File localDrive : roots){ System.out.println("System Drive : " + localDrive); } } }
Output…
System Drive : C:\ System Drive : D:\ System Drive : Q:\