Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › File I/O — java.io, NIO2 & Files API

What does StandardCopyOption.ATOMIC_MOVE give you, and why does it matter for safe file writes?

Level III · MidFile I/O — java.io, NIO2 & Files API
Short answer

ATOMIC_MOVE asks the filesystem to perform a rename as a single indivisible operation, so observers never see a partially written file — it either sees the old file or the complete new one.

Explanation

The standard safe-write pattern is to write to a temporary file, fsync it, then atomically move it over the target, which guarantees readers never observe a half-written or truncated file even if the process crashes mid-write. The caveat is atomicity is only guaranteed within the same filesystem; a cross-filesystem move can't be atomic and throws or degrades to copy-then-delete.

Common mistakes
  • Writing directly to the final file, leaving half-written data on crash.
  • Assuming ATOMIC_MOVE works across file systems.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for File I/O — java.io, NIO2 & Files API.

Practise free

More on File I/O — java.io, NIO2 & Files API