private static Writer initWriter(File file,Object encoding,boolean append) throws IOException {
  if (file == null) {
    throw new NullPointerException("File is missing");
  }
  if (encoding == null) {
    throw new NullPointerException("Encoding is missing");
  }
  boolean fileExistedAlready=file.exists();
  OutputStream stream=null;
  Writer writer=null;
  try {
    stream=new FileOutputStream(file,append);
    if (encoding instanceof Charset) {
      writer=new OutputStreamWriter(stream,(Charset)encoding);
    }
 else     if (encoding instanceof CharsetEncoder) {
      writer=new OutputStreamWriter(stream,(CharsetEncoder)encoding);
    }
 else {
      writer=new OutputStreamWriter(stream,(String)encoding);
    }
  }
 catch (  IOException ex) {
    IOUtils.closeQuietly(writer);
    IOUtils.closeQuietly(stream);
    if (fileExistedAlready == false) {
      FileUtils.deleteQuietly(file);
    }
    throw ex;
  }
catch (  RuntimeException ex) {
    IOUtils.closeQuietly(writer);
    IOUtils.closeQuietly(stream);
    if (fileExistedAlready == false) {
      FileUtils.deleteQuietly(file);
    }
    throw ex;
  }
  return writer;
}

private static Writer initWriter ( File file , Object encoding , boolean append ) throws IOException { if ( file == null ) { throw new NullPointerException ( "File is missing" ) ; } if ( encoding == null ) { throw new NullPointerException ( "Encoding is missing" ) ; } boolean fileExistedAlready = file . exists ( ) ; OutputStream stream = null ; Writer writer = null ; try { stream = new FileOutputStream ( file , append ) ; if ( encoding instanceof Charset ) { writer = new OutputStreamWriter ( stream , ( Charset ) encoding ) ; } else if ( encoding instanceof CharsetEncoder ) { writer = new OutputStreamWriter ( stream , ( CharsetEncoder ) encoding ) ; } else { writer = new OutputStreamWriter ( stream , ( String ) encoding ) ; } } catch ( IOException ex ) { IOUtils . closeQuietly ( writer ) ; IOUtils . closeQuietly ( stream ) ; if ( fileExistedAlready == false ) { FileUtils . deleteQuietly ( file ) ; } throw ex ; } catch ( RuntimeException ex ) { IOUtils . closeQuietly ( writer ) ; IOUtils . closeQuietly ( stream ) ; if ( fileExistedAlready == false ) { FileUtils . deleteQuietly ( file ) ; } throw ex ; } return writer ; }
initialise the wrapped file writer . ensure that a cleanup occurs if the writer creation fails .
