Java - File Root Listing

Introduction

To get a list of the available root directories in a file system, use the listRoots() static method of the File class.

It returns an array of File objects.

// Get the list of all root directories
File[] roots = File.listRoots();

The following code illustrates how to get the root directories.

Demo

import java.io.File;

public class Main {
  public static void main(String[] args) {
    File[] roots = File.listRoots();
    System.out.println("List of root directories:");
    for (File f : roots) {
      System.out.println(f.getPath());
    }/*from ww  w  . j  av  a  2s . co  m*/
  }
}

Result

Related Topic