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



