Java - List all drives letter in Window system

Posted on November 4, 2016


This example shows the use of listRoots() method of the File class. This method returns an array of File objects denoting the available filesystem roots, or null if the set of roots could not be determined. The array will be empty if there are no filesystem roots.

See the below example.

ListRootExample.java

package com.boraji.tutorial.io;

import java.io.File;

/**
 * @author imssbora
 * ListRootExample.java
 * Nov 4, 2016
 */
public class ListRootExample {
	public static void main(String[] args) {
		File[] files=File.listRoots();
		
		/*List of drives in window system*/
		for (File file : files) {
			System.out.println(file.toString());
		}
	}
}

Output

C:\
D:\
F:\
G:\