/** Copies a given XML file, and appends a given comment to the end */ private static void copyXmlWithComment( @NonNull File from, @NonNull File to, @Nullable String comment) throws IOException { int successfulOps = 0; InputStream in = new FileInputStream(from); try { FileOutputStream out = new FileOutputStream(to, false); try { ByteStreams.copy(in, out); successfulOps++; if (comment != null) { String commentText = "<!-- " + XmlUtils.toXmlTextValue(comment) + " -->"; byte[] suffix = commentText.getBytes(Charsets.UTF_8); out.write(suffix); } } finally { Closeables.close(out, successfulOps < 1); successfulOps++; } } finally { Closeables.close(in, successfulOps < 2); } }