public static boolean writeTextFile ( String _fileName , String _fileContent , Charset _charset , boolean _append ) { if ( StringUtil . isBlank ( _fileName ) ) { return false ; } String allText = "" ; if ( _append ) { File file = new File ( _fileName ) ; if ( file . exists ( ) ) { allText = readFileToString ( file ) + guessLineTerminatorOfFile ( _fileName ) ; } } allText += _fileContent ; OutputStreamWriter writer = null ; try { writer = new OutputStreamWriter ( new FileOutputStream ( _fileName ) , _charset ) ; writer . write ( allText ) ; } catch ( IOException _ex ) { LOGGER . error ( "Could not write file to '" + _fileName + "'" , _ex ) ; return false ; } finally { try { if ( writer != null ) { writer . close ( ) ; } } catch ( IOException _ex ) { LOGGER . error ( "Error while closing file '" + _fileName + "'" , _ex ) ; return false ; } } return true ; }
Write String to file with the given charset . Optionally appends the data to the file .