File listRoots() method in Java with examples
Last Updated :
28 Jan, 2019
The listRoots() method is a part of File class. The listRoots() function returns the root directories of all the available file System roots. It is guaranteed that the pathname of any file on the system will begin with any one of these roots.
Function Signature:
public static File[] listRoots()
Syntax:
File.listRoots()
Parameters: The function do not requires any parameters.
Return Value: The function returns File array, which contains all the file system roots .
Exception: This method do not throw any exceptions
Below programs will illustrate the use of listRoots() function:
Example 1: Try display all the root directories of the system
import java.io.*;
public class GFG {
public static void main(String args[])
{
File root[] = File.listRoots();
if (root != null ) {
System.out.print( "Roots are: " );
for ( int i = 0 ; i < root.length; i++) {
System.out.print(root[i].getPath() + " " );
}
}
else
System.out.println( "There are no roots" );
}
}
|
Output:
Roots are: C:\ D:\ E:\ F:\ G:\
The programs might not run in an online IDE. please use an offline IDE and set the path of the file
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...