Java File listRoots() method example

HQ ┬╗ Java Tutorial ┬╗ ┬╗ File ┬╗ listRoots() method example
Java File listRoots() method example 2018-02-16T07:26:40+00:00 listRoots()


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

Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.