Skip to main content

Checkpoint: append, rotate, replay

Challenge on lesson: Checkpoint: The JSONL Journal

CHALLENGE
Difficulty: advanced+25 XP

Inside Solution, implement the journal over a temp directory you create in each call: 1. static Path append(Path file, String event) โ€” appends event + "\n" in UTF-8 with CREATE + APPEND; returns the file. 2. static List<String> replay(Path file) โ€” returns every complete line (a TRUNCATED final line โ€” one lacking its newline โ€” must be dropped; use Files.readString and split on "\n" carefully, or readAllLines and drop a final fragment you detect by re-reading raw bytes). 3. static Path rotate(Path file, int maxLines) โ€” if the file holds more than maxLines complete lines, move it to a sibling named by appending ".1" (file.1) atomically, and leave a fresh empty file at the original path; return the ARCHIVE path (or the original if no rotation happened). 4. static int countEvents(Path file) โ€” number of complete lines.

Back to lesson: Checkpoint: The JSONL Journal