### Question:
Files { public static void atomicReplace(Path path, byte[] bytes) throws IOException { requireNonNull(bytes); Path directory = path.getParent(); java.nio.file.Files.createDirectories(directory); Path tempFile = java.nio.file.Files.createTempFile(directory, "." + path.getName(0), ".tmp"); try { java.nio.file.Files.write(tempFile, bytes); java.nio.file.Files.move(tempFile, path, REPLACE_EXISTING, ATOMIC_MOVE); } catch (Throwable e) { try { java.nio.file.Files.delete(tempFile); } catch (IOException e2) { e.addSuppressed(e2); } throw e; } } private  Files(); static boolean createFileIfMissing(Path path, FileAttribute<?>... attrs); static void deleteRecursively(Path directory); static Path copyResource(String resourceName, Path destination, OpenOption... options); static Path copyResource(ClassLoader classloader, String resourceName, Path destination, OpenOption... options); static long copyResource(String resourceName, OutputStream out); static long copyResource(ClassLoader classloader, String resourceName, OutputStream out); static void atomicReplace(Path path, byte[] bytes); static void atomicReplace(Path path, IOConsumer<Writer> fn); static void atomicReplace(Path path, Charset charset, IOConsumer<Writer> fn);  }### Answer:
@Test void canReplaceAtomic(@TempDirectory Path tempDir) throws Exception { Files.createDirectories(tempDir.resolve("org/something")); Files.write(tempDir.resolve("org/something/test.txt"), "foo".getBytes(StandardCharsets.UTF_8)); atomicReplace(Paths.get("org/something/test.txt"), "hello world".getBytes(StandardCharsets.UTF_8)); assertEquals(11, Files.size(Paths.get("org/something/test.txt"))); }
@Test void canReplaceAtomicWriter(@TempDirectory Path tempDir) throws Exception { Files.createDirectories(tempDir.resolve("org/something")); Files.write(tempDir.resolve("org/something/test.txt"), "foo".getBytes(StandardCharsets.UTF_8)); atomicReplace(Paths.get("org/something/test.txt"), (writer) -> { writer.write("hello world"); }); assertEquals(11, Files.size(Paths.get("org/something/test.txt"))); }