/** Write a file safely - the change happens (the function returns true) or * somthing went wrong (the function throws a runtime exception) and the file is not changed. * Note that the tempfile must be in the same direct as the actual file so an OS-atomic rename can be done. */ public static boolean safeWrite(Path file, Path tmpFile, IOConsumer<OutputStream> writerAction) { try { try(OutputStream out = new BufferedOutputStream(Files.newOutputStream(tmpFile)) ) { writerAction.actionEx(out); } move(tmpFile, file); return true; } catch(IOException ex) { throw IOX.exception(ex); } }