Sunday, 25 June 2017

List all Local Drives in Java ~ foundjava

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

No comments:

Post a Comment