liveBook · Manning

File system

As explained in 100 Java Mistakes and How to Avoid Them

A filesystem is a system that organizes and provides access to files and directories on storage or through a mounted hierarchy.

Treat filesystem state as changeable between operations. A check followed by a separate use can observe a different file, directory, or size after another process changes the path. section 9.11

Avoid separate filesystem queries

Checking isRegularFile(path) and then calling size(path) allows a process to replace the file with a directory between calls. readAttributes() obtains the relevant attributes through one filesystem query, making the size decision more robust. section 9.11

Do not trust a checked size

Files.size(path) followed by readAllBytes(path) can load a file that grew after the size check. Open the stream first, read up to the limit, and detect additional data while reading.

Reduce filesystem race windows by:

  • Combining related metadata requests with Files.readAttributes(). section 9.11

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains separate queries, combined attributes, and bounded reads, but treats replacement races as corner cases rather than internals.
Examples
Uses backup-folder time checks, file-type and size queries, and maximum-size file reads.
What it assumes
Assumes Java familiarity and introduces no filesystem fundamentals beyond practical Files operations.
What you can do after
Apply safer Java NIO file handling by combining metadata queries and enforcing size limits during reads.
How it connects
Connects filesystem operations with flaky tests, file attributes, atomic queries, streams, and concurrent process changes.

Also explained in

Compare how 4 books explain file system →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.