Home Java Files Methods getFileStore() Java Files getFileStore() Method 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:
FileStore store = Files.getFileStore(path);
boolean ro = store.isReadOnly();
boolean posix = store.supportsFileAttributeView("posix"); Call What 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 public static FileStore getFileStore(Path path) throws IOException Parameters Parameter Description pathAn existing file or folder.
Return Value Return Value Description FileStoreThe store that holds the file.
Compare with Similar Methods Related methods:
Java Examples 1. Writable volume? 2. Which views exist? 3. Same volume? Example 1 — Check That the Temp Volume Is Writable Ask the store whether it is read-only and whether it supports the basic view.
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);
}
} Run with java GetFileStoreReadOnly. The output is the same on Windows, macOS, and Linux.
Read-only volume: false
Supports basic view: true How It Works The temp volume. The file lives in the system temp folder, which is always on a writable volume.Volume-wide answer. isReadOnly describes the whole store, such as a CD or a read-only mount, not one file.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.
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);
}
} Run with java GetFileStoreViews. The first part is the output on Linux or macOS; the last part shows Windows, which has no POSIX permissions.
basic: true
posix: true
--- On Windows (NTFS) ---
basic: true
posix: false How It Works Ask before using. Checking the view first avoids an UnsupportedOperationException from getPosixFilePermissions.Per volume. One computer can mix stores: a Linux machine with a FAT USB stick has posix on one and not the other.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.
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);
}
} Run with java GetFileStoreSameVolume. The output is the same on Windows, macOS, and Linux.
Same volume: true
missing: NoSuchFileException How It Works Sibling folders. inbox and done sit side by side in the temp folder, so they share a store.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.Must exist. A path that does not exist has no store, so getFileStore throws NoSuchFileException.Frequently Asked Questions How do I find which drive a file is on in Java? Files.getFileStore(path) returns its FileStore; name() and type() describe it.
How do I check free disk space in Java? Files.getFileStore(path).getUsableSpace() returns the bytes available to this program.
How do I know if a volume is read-only? Files.getFileStore(path).isReadOnly().
How can I tell if two paths are on the same drive? Compare Files.getFileStore(a).equals(Files.getFileStore(b)).
Does getFileStore work for a file that does not exist yet? No. Pass its existing parent folder instead.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion