Java 101Listing a file system's root directories

time to read 1 min | 77 words

Using the static FileSystems class, you can get the default FileSystem (note the missing s) through the getDefault() method.

The getRootDirectories() method of FileSystem can be used to acquire a list of root directories on a file system.

An object of type Iterable is returned, so the directories can be iterated over in the following way:

FileSystem sys = FileSystems.getDefault();
Iterable<Path> d = sys.getRootDirectories();
for (Path name: d) {
    System.out.println(name);
}

This is available from Java 1.7.