Friday, September 26, 2025
HomeLanguagesJavaFileSystem getFileStores() Method in Java with Examples

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:




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

RELATED ARTICLES

Most Popular

Dominic
32320 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6683 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6756 POSTS0 COMMENTS
Umr Jansen
6762 POSTS0 COMMENTS