
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - File listRoots()method
Description
The Java File listRoots() method returns an array of File objects representing the available filesystem roots (drive letters on Windows or mount points on Linux/macOS). It helps list all the root directories present on the system.
Declaration
Following is the declaration for java.io.File.listRoots() method −
public static File[] listRoots()
Parameters
filter− File filter
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 - Usage of File listRoots() method
The following example shows the usage of Java File listRoots() method. We've created an array of files as File[] paths. Then we're getting all the roots in the current file system using File.listRoots() method call. As next step, we've iterated the result and print each path. In case of any exception during invocation of this method, we've handled the exception and printed the stack trace to figure out the root cause.
FileDemo.java
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(); } } }
Output
Let us compile and run the above program, this will produce the following result(depends on system's free space)−
C:\ F:\
Example - Listing All Root Directories
The following example shows the usage of Java File listRoots() method.
FileDemo.java
package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { // Get an array of filesystem roots File[] roots = File.listRoots(); // Print each root directory System.out.println("Available filesystem roots:"); for (File root : roots) { System.out.println(root.getAbsolutePath()); } } }
Output on Windows
Let us compile and run the above program, this will produce the following result−
Available filesystem roots: C:\ D:\
Output on Linux\MacOS
Let us compile and run the above program, this will produce the following result−
Available filesystem roots: /
Explanation
The File.listRoots() method returns an array of root directories.
The program loops through the array and prints each root directory.
Example - Listing Root Directories with Free Space Information
The following example shows the usage of Java File listRoots() method.
FileDemo.java
package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { // Get an array of filesystem roots File[] roots = File.listRoots(); // Print each root directory along with free space System.out.println("Root directories and available space:"); for (File root : roots) { long freeSpaceGB = root.getFreeSpace() / (1024 * 1024 * 1024); System.out.println(root.getAbsolutePath() + " - Free space: " + freeSpaceGB + " GB"); } } }
Output on Windows
Let us compile and run the above program, this will produce the following result (depends on system's free space)−
Root directories and available space: C:\ - Free space: 150 GB D:\ - Free space: 500 GB
Output on Linux/MacOS
Let us compile and run the above program, this will produce the following result (depends on system's free space) −
Root directories and available space: / - Free space: 200 GB
Explanation
The File.listRoots() method gets all filesystem root directories.
The program calculates and prints the free space for each root directory.
On Windows, listRoots() returns drive letters like C:\, D:\, etc.
On Linux/macOS, it usually returns / as the only root.