How To Get Operating System Drive Letters in Java

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:\

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*