Skip to main content

Path semantics and atomic operations

advanced15 min readLesson 157 of 180

Immutable paths, resolve/relativize, and operations that carry their guarantees in their signature.

Path is an immutable description of a file location — not a handle to an open file, not a promise the file exists.

Path base = Path.of("/tmp", "cjadv");
Path p = base.resolve("logs/app.log");   // /tmp/cjadv/logs/app.log
p.getParent();  p.getFileName();         // navigation
base.relativize(p);                      // logs/app.log

resolve joins; relativize is the inverse. The sandbox gives each run a writable /tmp — every file operation in this course targets Files.createTempDirectory("cjadv") so runs never collide and never touch anything outside their sandbox.

Existence is a state, not a property: between Files.exists(p) and Files.createFile(p) another actor can create the file. The NIO answer is atomic operations with semantics, not check-then-act: Files.createFile(p) throws if it exists (race-safe creation); Files.move(src, dst, REPLACE_EXISTING, ATOMIC_MOVE) either swaps completely or throws — no half-copied files ever observable.