Library contents

Java File roots and disk space

Inspect the filesystem roots and obtain estimates of total, free, and usable bytes.

A simple example

File roots and disk space

  1. Start with Example volume with 10 units total and 4 available
  2. Do this Compare a 3-unit request with available space
  3. Result It fits
The available-space reading is a snapshot. Three is below four in this illustration.

Syntax

Java syntax
public static File[] listRoots()
JDK 21 declarations (4)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Check disk capacity before exporting a video

A media student is about to render a video to a local volume. The application needs the size of that volume and the available capacity for this account, while avoiding hard-coded drive letters or disk sizes.

First output linetrue
Main.java
import java.nio.file.*;
import java.io.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            File volume = desk.toFile();
            File[] roots = File.listRoots();
            System.out.println(roots != null && roots.length > 0);
            System.out.println(volume.getTotalSpace() > 0);
            System.out.println(volume.getFreeSpace() >= 0 && volume.getUsableSpace() >= 0);
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
true
true
true

The output checks properties of the measured values rather than printing machine-specific byte counts.

Advertisements

Compare two folders on one disk

A media lab keeps preview and export folders below the same local workspace. Before adding reported capacities together, check whether those folders are reporting the capacity of the same volume.

Resulttrue
Main.java
import java.nio.file.*;
import java.io.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            File preview = Files.createDirectory(desk.resolve("preview")).toFile();
            File export = Files.createDirectory(desk.resolve("export")).toFile();
            System.out.println(preview.getTotalSpace() == export.getTotalSpace());
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
true

These new sibling folders are on the same volume and report the same total capacity.

Prepare a gallery label

A curator stores a short UTF-8 caption beside an exhibit. On a drive with room to spare, compare its available byte count with the size of one extra copy. Can this copy fit?

GivenFile contents “clay”
Worked resultFits

What to watch for

Space values can change between calls and depend on quotas and permissions. Usable space is an estimate available to this process.

Documentation for File roots and disk space