Java Files getFileStore() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java, Files.getFileStore(path) returns the FileStore that contains the file: a drive such as C: on Windows, or a mounted partition on Linux and macOS.

The store tells you whether it is read-only (isReadOnly()), which attribute views it supports (supportsFileAttributeView), and how much space it has. Comparing the stores of two paths shows whether they are on the same volume.

A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.getFileStore(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Pass an existing path:

Java
FileStore store = Files.getFileStore(path);
boolean ro = store.isReadOnly();
boolean posix = store.supportsFileAttributeView("posix");
CallWhat it does
store.isReadOnly()Can anything be written there?
store.supportsFileAttributeView(v)Is a view such as posix available?
store.equals(other)Same volume?

Since Java 7. A store also reports name(), type(), and its total and usable space, which differ per machine and are not printed here.

Library declarations
Java
public static FileStore getFileStore(Path path) throws IOException

Parameters

ParameterDescription
pathAn existing file or folder.

Return Value

Return ValueDescription
FileStoreThe store that holds the file.

Compare with Similar Methods

Related methods:

MethodHow it differs
move()Fast rename on one store, copy across stores.
isWritable()Per-file write check.
readAttributes()Uses the views the store supports.

Java Examples

Example 1 — Check That the Temp Volume Is Writable

Ask the store whether it is read-only and whether it supports the basic view.

Java
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;

public class GetFileStoreReadOnly {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("probe.txt"), new byte[0]);
        FileStore store = Files.getFileStore(file);
        System.out.println("Read-only volume: " + store.isReadOnly());
        System.out.println("Supports basic view: " + store.supportsFileAttributeView(BasicFileAttributeView.class));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. The temp volume. The file lives in the system temp folder, which is always on a writable volume.
  2. Volume-wide answer. isReadOnly describes the whole store, such as a CD or a read-only mount, not one file.
  3. basic is universal. Every store supports BasicFileAttributeView; the name, type, and free space are left out because they differ per machine.

Example 2 — Does the Volume Support POSIX Attributes?

Ask the store about views by name before using them.

Java
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;

public class GetFileStoreViews {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        FileStore store = Files.getFileStore(dir);
        for (String view : new String[] {"basic", "posix"}) {
            System.out.println(view + ": " + store.supportsFileAttributeView(view));
        }
        Files.delete(dir);
    }
}

How It Works

  1. Ask before using. Checking the view first avoids an UnsupportedOperationException from getPosixFilePermissions.
  2. Per volume. One computer can mix stores: a Linux machine with a FAT USB stick has posix on one and not the other.
  3. Typical results. Linux and macOS volumes report posix; NTFS on Windows does not, but supports dos and acl instead.

Example 3 — Are Two Folders on the Same Volume?

Equal stores mean a move between them can be a fast rename. A missing path has no store.

Java
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;

public class GetFileStoreSameVolume {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path inbox = Files.createDirectory(dir.resolve("inbox"));
        Path done = Files.createDirectory(dir.resolve("done"));
        FileStore a = Files.getFileStore(inbox);
        FileStore b = Files.getFileStore(done);
        System.out.println("Same volume: " + a.equals(b));
        try {
            Files.getFileStore(dir.resolve("missing"));
        } catch (NoSuchFileException e) {
            System.out.println("missing: NoSuchFileException");
        }
        Files.delete(inbox);
        Files.delete(done);
        Files.delete(dir);
    }
}

How It Works

  1. Sibling folders. inbox and done sit side by side in the temp folder, so they share a store.
  2. Why it matters. Files.move on one store is a quick rename; across stores it copies and deletes, which is slower and cannot be atomic.
  3. Must exist. A path that does not exist has no store, so getFileStore throws NoSuchFileException.

Frequently Asked Questions

Files.getFileStore(path) returns its FileStore; name() and type() describe it.
Files.getFileStore(path).getUsableSpace() returns the bytes available to this program.
Files.getFileStore(path).isReadOnly().
Compare Files.getFileStore(a).equals(Files.getFileStore(b)).
No. Pass its existing parent folder instead.

Next: Files.getLastModifiedTime()

Get a file's last-modified time as a FileTime.

getLastModifiedTime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful