Java.io.File.listRoots() Method
Advertisements
Description
The java.io.File.listRoots() returns an array of abstract pathnames indicating the files and directories in the directory indicated by this abstract pathname that satisfy the specified filter.
Declaration
Following is the declaration for java.io.File.listRoots() method:
public static File[] listRoots()
Parameters
NA
Return Value
The method returns an array of File Objects indicating the available file system roots. The method returns null, if the set of roots could not be determined.
Exception
NA
Example
The following example shows the usage of java.io.File.listRoots() method.
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File[] paths;
try{
// returns pathnames for files and directory
paths = File.listRoots();
// for each pathname in pathname array
for(File path:paths)
{
// prints file and directory paths
System.out.println(path);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
C:\ D:\ E:\ F:\ G:\ J:\