List all Local Drives in Java

An example on getting paths of all local drives in a computer in Java.

Get Local Drive Paths - Example



import java.io.File;
class GetLocalDrives
{
public static void main(String args[])
{
File[] f=File.listRoots();
for(File k:f)
System.out.println(k);
}
}


listRoots(): It is a static method of the java.io.File class and is used to get all the roots (local drives).


Sample Output



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


Posted by at on

Tags: Java IO,