
- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display root directories in the system using Java
Problem Description
How to display root directories in the system?
Solution
Following example shows how to find root directories in your system using listRoots() method of File class.
import java.io.*; public class Main { public static void main(String[] args) { File[] roots = File.listRoots(); System.out.println("Root directories in your system are:"); for (int i = 0; i < roots.length; i++) { System.out.println(roots[i].toString()); } } }
Result
The above code sample will produce the following result. The result may vary depending on the system & operating system. If you are using Unix system you will get a single root '/'.
Root directories in your system are: C:\ D:\ E:\ F:\ G:\ H:\
java_directories.htm
Advertisements