return newWriter(self, charset, append, false);
/** * Creates a buffered writer for this file without writing a BOM, writing data using the given * encoding. * * @param self a Path * @param charset the name of the encoding used to write in this file * @return a BufferedWriter * @throws java.io.IOException if an IOException occurs. * @since 2.3.0 */ public static BufferedWriter newWriter(Path self, String charset) throws IOException { return newWriter(self, charset, false); }
/** * Helper method to create a buffered writer for a file without writing a BOM. * * @param self a Path * @param charset the name of the encoding used to write in this file * @param append true if in append mode * @return a BufferedWriter * @throws java.io.IOException if an IOException occurs. * @since 2.3.0 */ public static BufferedWriter newWriter(Path self, String charset, boolean append) throws IOException { return newWriter(self, charset, append, false); }
/** * Create a new PrintWriter for this file. * * @param self a Path * @return the created PrintWriter * @throws java.io.IOException if an IOException occurs. * @since 2.3.0 */ public static PrintWriter newPrintWriter(Path self) throws IOException { return new GroovyPrintWriter(newWriter(self)); }
/** * Create a new PrintWriter for this file, using specified * charset. * * @param self a Path * @param charset the charset * @return a PrintWriter * @throws java.io.IOException if an IOException occurs. * @since 2.3.0 */ public static PrintWriter newPrintWriter(Path self, String charset) throws IOException { return new GroovyPrintWriter(newWriter(self, charset)); }
private static void appendBuffered(Path file, Object text, String charset, boolean writeBom) throws IOException { BufferedWriter writer = null; try { boolean shouldWriteBom = writeBom && !file.toFile().exists(); writer = newWriter(file, charset, true); if (shouldWriteBom) { IOGroovyMethods.writeUTF16BomIfRequired(writer, charset); } InvokerHelper.write(writer, text); writer.flush(); Writer temp = writer; writer = null; temp.close(); } finally { closeWithWarning(writer); } }
/** * Creates a new BufferedWriter for this file, passes it to the closure, and * ensures the stream is flushed and closed after the closure returns. * The writer will use the given charset encoding. If the given charset is * "UTF-16BE" or "UTF-16LE" (or an equivalent alias), <code>writeBom</code> * is <code>true</code>, and the file doesn't already exist, the requisite * byte order mark is written to the stream when the writer is created. * * @param self a Path * @param charset the charset used * @param writeBom whether to write the BOM * @param closure a closure * @return the value returned by the closure * @throws java.io.IOException if an IOException occurs. * @since 2.5.0 */ public static <T> T withWriter(Path self, String charset, boolean writeBom, @ClosureParams(value = SimpleType.class, options = "java.io.Writer") Closure<T> closure) throws IOException { return IOGroovyMethods.withWriter(newWriter(self, charset, false, writeBom), closure); }
/** * Create a new BufferedWriter which will append to this * file. The writer is passed to the closure and will be closed before * this method returns. The writer will use the given charset encoding. * If the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), * <code>writeBom</code> is <code>true</code>, and the file doesn't already exist, * the requisite byte order mark is written to the stream when the writer is created. * * @param self a Path * @param charset the charset used * @param writeBom whether to write the BOM * @param closure a closure * @return the value returned by the closure * @throws java.io.IOException if an IOException occurs. * @since 2.5.0 */ public static <T> T withWriterAppend(Path self, String charset, boolean writeBom, @ClosureParams(value = SimpleType.class, options = "java.io.Writer") Closure<T> closure) throws IOException { return IOGroovyMethods.withWriter(newWriter(self, charset, true, writeBom), closure); }