Open In App

FileSystem getFileStores() Method in Java with Examples

Last Updated : 10 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The getFileStores() method of java.nio.file.FileSystem is used to return an iterable of FileStore object to iterate over the underlying file stores. The elements contained by the returned iterator are the FileStores for this file system. When an input-output error occurs, because of the inaccessibility to a file store, then it is not returned by the iterator.

Syntax:

public abstract Iterable<FileStore> getFileStores()

Parameters: This method does not accept anything.

Return value: This method returns an object to iterate over the backing file stores.

Below programs illustrate getFileStores() method:
Program 1:




// Java program to demonstrate
// java.nio.file.FileSystem.getFileStores() method
  
import java.nio.file.*;
import java.util.Iterator;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of Path
        Path path = Paths.get("C:\\Users\\"
                              + "asingh.one\\Documents");
  
        // get FileSystem object
        FileSystem fs = path.getFileSystem();
  
        // apply getFileStores() methods
        Iterable<FileStore> it = fs.getFileStores();
  
        // print all FileStore contains by this system
        Iterator<FileStore> iterator = it.iterator();
        System.out.println("FileStrores are:\n");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}


Output:

Program 2:




// Java program to demonstrate
// java.nio.file.FileSystem.getFileStores() method
  
import java.nio.file.*;
import java.util.Iterator;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of Path
        Path path = Paths.get("D:\\eclipse");
  
        // get FileSystem object
        FileSystem fs = path.getFileSystem();
  
        // apply getFileStores() methods
        Iterable<FileStore> it = fs.getFileStores();
  
        // print all FileStore contains by this system
        Iterator<FileStore> iterator = it.iterator();
        System.out.println("FileStores on system are:\n");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}


Output:

References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/FileSystem.html#getFileStores()



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads