Java File.listRoots() method example

 Posted On  | Pooja Lakkihal 

In this java tutorial, You will learn how to get the available filesystem roots. Java File.listRoots() method returns an array of File objects denoting the available filesystem roots, or null if the set of roots could not be determined.

List the available filesystem roots. This method returns an array of File objects that denote the root directories of the available filesystem roots.

syntax

public static File[] listRoots()

Returns

An array of File objects denoting the available filesystem roots, or null if the set of roots could not be determined.

Java File listRoots() example

  • File[] roots = File.listRoots(); List the available filesystem roots.
  • Print all the roots in the system in for loop.
package com.technicalkeeda;

import java.io.File;

public class App {

    public static void main(String[] args) {

        File[] roots = File.listRoots();

        System.out.println("List of available filesystem roots:- ");

        for (File root: roots) {

            System.out.println(root);
        }

    }
}

output

List of available filesystem roots:- 

C:\
D:\
E:\


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0725 seconds.