public static File appendToFile ( String filename , String extraContent , boolean onNewLine ) { PrintWriter pw = null ; try { pw = new PrintWriter ( new BufferedWriter ( new OutputStreamWriter ( new FileOutputStream ( filename , true ) , FILE_ENCODING ) ) ) ; if ( onNewLine ) { pw . println ( ) ; } pw . print ( extraContent ) ; } catch ( FileNotFoundException e ) { throw new IllegalArgumentException ( "Unable to write to: " + filename , e ) ; } catch ( UnsupportedEncodingException e ) { throw new RuntimeException ( e ) ; } finally { if ( pw != null ) { pw . close ( ) ; } } return new File ( filename ) ; }
Appends the extra content to the file in UTF - 8 encoding .