FileSystem getFileStores() Method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
        }
    }
}

chevron_right


Output:

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.