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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!