Skip to content
Rezha Julio
Go back

Listing a file system's root directories

1 min read

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.


Related Posts


Previous Post
Translating Scanner tokens into primitive types
Next Post
The Console class