Saturday, 8 June 2013

Java code to display local hard drive details

Code
----------
import java.io.File;

class GetLocalDrives {
public static void main(String args[]) {
File[] f = File.listRoots();
for (File k : f)
System.out.println(k);
System.out.println("The file separator is " + File.separatorChar);
}
}

Sample output
------------------
C:\
D:\
E:\
F:\
H:\
The file separator is \

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

 Note: The file separator depends upon your OS. If windows \ is the output, for linux like OS / is the file separator.

No comments:

Post a Comment