How do I get the list of file system root?

The code below demonstrate how to obtain file system root available on your operating system. In Linux you’ll have a single root “/” while on Windows you could get C: or D: that represent the root of the C and D 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 (int i = 0; i < root.length; i++) {
            File file = root[i];
            System.out.println("Root: " + file.getAbsolutePath());
        }
    }
}

Wayan

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

Leave a Reply