Wednesday, June 10, 2026
HomeLanguagesJavaFiles getFileStore() method in Java with Examples

Files getFileStore() method in Java with Examples

getFileStore() method of java.nio.file.Files help us to return the FileStore object which represents the file store where a file is located. Once you get a reference to the FileStore you can apply filestore type of operation to get information of filestore. Syntax:

public static FileStore
  getFileStore(Path path)
    throws IOException

Parameters: This method accepts a parameter path which is the path to the file to get FileStore. Return value: This method returns the file store where the file is stored. Exception: This method will throw following exceptions:

  1. IOException: if an I/O error occurs
  2. SecurityException: In the case of the default provider, and a security manager is installed, the SecurityManager.checkgetFileStore(String) method is invoked to check to getFileStore access to the file

Below programs illustrate getFileStore(Path) method: Program 1: 

Java




// Java program to demonstrate
// Files.getFileStore() method
 
import java.io.IOException;
import java.nio.file.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create object of Path
        Path path
            = Paths.get(
                "D:\\Work\\Test\\file1.txt");
 
        // get FileStore object
        try {
 
            FileStore fs
                = Files.getFileStore(path);
 
            // print FileStore name and block size
            System.out.println("FileStore Name: "
                               + fs.name());
            System.out.println("FileStore BlockSize: "
                               + fs.getBlockSize());
        }
        catch (IOException e) {
 
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


Output:

Program 2: 

Java




// Java program to demonstrate
// Files.getFileStore() method
 
import java.io.IOException;
import java.nio.file.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create object of Path
        Path path = Paths.get("C:\\data\\db");
 
        // get FileStore object
        try {
 
            FileStore fs
                = Files.getFileStore(path);
 
            // print FileStore details
            System.out.println("FileStore:"
                               + fs.toString());
            System.out.println("FileStore Free Space: "
                               + fs.getUnallocatedSpace()
                               + " Bytes");
        }
        catch (IOException e) {
 
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


Output:

Reference: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html#getFileStore(java.nio.file.Path)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS