- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
List the file system roots in Java
The method java.io.File.listRoots() is used to list the file system roots in Java. This method requires no parameters. It returns the available file system roots in the form of an array of file objects and if the file system roots cannot be determined, it returns null.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { File[] roots = File.listRoots(); try { for(File r : roots) { System.out.println(r); } } catch(Exception e) { e.printStackTrace(); } } }
The output of the above program is as follows −
Output
C:\ D:\ E:\ F:\
Note − The output may vary on Online Compilers.
Now let us understand the above program.
The method java.io.File.listRoots() is used to list the file system roots in the form of an array. Then these array elements are printed using for loop. A code snippet that demonstrates this is given as follows −
File[] roots = File.listRoots(); try { for(File r : roots) { System.out.println(r); } } catch(Exception e) { e.printStackTrace(); }
Advertisements