On this document we will be showing a java example on how to use the listRoots() method of File Class. This method list the available filesystem roots.

A particular Java platform may support zero or more hierarchically-organized file systems. Each file system has a root directory from which all other files in that file system can be reached. Windows platforms, for example, have a root directory for each active drive; UNIX platforms have a single root directory, namely “/”. The set of available filesystem roots is affected by various system-level operations such as the insertion or ejection of removable media and the disconnecting or unmounting of physical or virtual disk drives.

Method Syntax

public static File[] listRoots()

Method Argument

Data Type Parameter Description

Method Returns

This method returns an array of File objects denoting the available filesystem roots, or null if the set of roots could not be determined. The array will be empty if there are no filesystem roots.


Requires Java 1.2 and up

Java File listRoots() Example

Below is a java code demonstrates the use of listRoots() method of File class. The example presented might be simple however it shows the behaviour of the listRoots() method of File class. Basically we just printed out the the return value of the listRoots() method which just a list of the drive that we have in our machine.

Sample Output

Below is the sample output when you run the above example.

java lang File listRoots() example output