java.io.File listRoots()
Description
A particular Java platform may support zero or more hierarchically-organized file systems. Each file system has a root directory from which all other files in that file system can be reached. Windows platforms, for example, have a root directory for each active drive; UNIX platforms have a single root directory, namely “/”. The set of available filesystem roots is affected by various system-level operations such as the insertion or ejection of removable media and the disconnecting or unmounting of physical or virtual disk drives.
Method Syntax
public static File[] listRoots()
Method Argument
Data Type | Parameter | Description |
---|---|---|
N/A | N/A | N/A |
Method Returns
This method returns an array of File objects denoting the available filesystem roots, or null if the set of roots could not be determined. The array will be empty if there are no filesystem roots.
Compatibility
Requires Java 1.2 and up
Java File listRoots() Example
Below is a java code demonstrates the use of listRoots() method of File class. The example presented might be simple however it shows the behaviour of the listRoots() method of File class. Basically we just printed out the the return value of the listRoots() method which just a list of the drive that we have in our machine.
package com.javatutorialhq.java.examples; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; /* * This example source code demonstrates the use of * listFiles(FilenameFilter filter) method of File class. * */ public class FileListFilesFilenameFilterExample { public static void main(String[] args) { // initialize File object File file = new File("C:javatutorialhq"); // check if the specified pathname is directory first if(file.isDirectory()){ //list all files on directory File[] files = file.listFiles(new FilenameFilter() { //apply a filter @Override public boolean accept(File dir, String name) { boolean result; if(name.startsWith("test")){ result=true; } else{ result=false; } return result; } }); //print all files on the directory for(File f:files){ try { System.out.println(f.getCanonicalPath()); } catch (IOException e) { e.printStackTrace(); } } } } }