public static BufferedWriter newWriter(File file, String charset, boolean append, boolean writeBom) throws IOException {
        boolean shouldWriteBom = writeBom && !file.exists();
        if (append) {
            FileOutputStream stream = new FileOutputStream(file, append);
            if (shouldWriteBom) {
                IOGroovyMethods.writeUTF16BomIfRequired(stream, charset);
            }
            return new EncodingAwareBufferedWriter(new OutputStreamWriter(stream, charset));
        } else {
            FileOutputStream stream = new FileOutputStream(file);
            if (shouldWriteBom) {
                IOGroovyMethods.writeUTF16BomIfRequired(stream, charset);
            }
            return new EncodingAwareBufferedWriter(new OutputStreamWriter(stream, charset));
        }
    }
public static BufferedWriter newWriter ( File file , String charset , boolean append , boolean writeBom ) throws IOException { boolean shouldWriteBom = writeBom && ! file . exists ( ) ; if ( append ) { FileOutputStream stream = new FileOutputStream ( file , append ) ; if ( shouldWriteBom ) { IOGroovyMethods . writeUTF16BomIfRequired ( stream , charset ) ; } return new EncodingAwareBufferedWriter ( new OutputStreamWriter ( stream , charset ) ) ; } else { FileOutputStream stream = new FileOutputStream ( file ) ; if ( shouldWriteBom ) { IOGroovyMethods . writeUTF16BomIfRequired ( stream , charset ) ; } return new EncodingAwareBufferedWriter ( new OutputStreamWriter ( stream , charset ) ) ; } }
Helper method to create a buffered writer for a file.  If the given
charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias), the
requisite byte order mark is written to the stream before the writer
is returned.

@param file     a File
@param charset  the name of the encoding used to write in this file
@param append   true if in append mode
@param writeBom whether to write a BOM
@return a BufferedWriter
@throws IOException if an IOException occurs.
@since 2.5.0