How do I get the list of file system root?

The code below demonstrates how to obtain file system root available on your system. In Linux you will have a single root (/) while on Windows you could get C:\ or D:\ that represent the root drives.

package org.kodejava.example.io;

import java.io.File;

public class FileSystemRoot {
    public static void main(String[] args) {
        // List the available filesystem roots.
        File[] root = File.listRoots();

        // Iterate the entire filesystem roots.
        for (File file : root) {
            System.out.println("Root: " + file.getAbsolutePath());
        }
    }
}

The result of the code snippet:

Root: C:\
Root: D:\

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.