Thursday, October 05, 2006

Drive Names in Windows

Have you ever thought how to get drive letters in Windows? If you know the cool hack below, you wont need to struggle for that.

import java.io.File;

/**
* @author karthikkannan
* Demo for listing drive letters on Windows
*/
public class Utility {
public static void main(String[] args) {
File[] roots = getDrives();
for (File file : roots) {
System.out.println(file.getPath());
}
}

public static File[] getDrives() {
return File.listRoots();
}
}

There is a static method called listRoots() in the java.io.File class. Hope this was a useful tip. See you in the next post folks.

Sources: http://exampledepot.com/

No comments: