FileStore Queries Disk Space And Capacity

Read total, unallocated, and usable space from the volume holding the working directory so a disk-full condition becomes a planned degradation.

JavaResilience

Code

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

public class DiskCapacityProbe {

    public static void main(String[] args) throws IOException {
        // the volume holding the current working directory
        FileStore store = Files.getFileStore(Path.of("."));

        long total = store.getTotalSpace();
        long unallocated = store.getUnallocatedSpace();
        long usable = store.getUsableSpace();

        System.out.println("store       : " + store.name() + " (" + store.type() + ")");
        System.out.println("total MiB   : " + total / (1024 * 1024));
        System.out.println("unalloc MiB : " + unallocated / (1024 * 1024));
        System.out.println("usable MiB  : " + usable / (1024 * 1024));

        // usable <= unallocated: usable also subtracts quotas and other reservations
        // probe capacity before writing, turning "disk full" into a predictable degradation instead of a mid-run IOException
        if (usable < 64L * 1024 * 1024) {
            System.out.println("low disk: refuse to start background writer");
        }
    }
}

Inside the snippet

FileStore Queries Disk Space And Capacity keeps everything in the class DiskCapacityProbe.

Putting it to use

The run emits 5 labeled sections — store :, total MiB :, unalloc MiB :, usable MiB :, low disk: refuse to start background writer — one per step.

Core lines

FileStore store = Files.getFileStore(Path.of("."));
long unallocated = store.getUnallocatedSpace();
long usable = store.getUsableSpace();

Interactive Tools for This Topic

FAQ

What does FileStore Queries Disk Space And Capacity print when run?
No setup needed: run the file and look for labeled lines like `store :`, `total MiB :`, `unalloc MiB :` in the output.