public static void writeObjectToFile ( String outputFileName , Object toWrite , boolean append ) { // Open the output file. Writer resultWriter ; try { resultWriter = new FileWriter ( outputFileName , append ) ; } catch ( IOException e ) { throw new IllegalStateException ( "Unable to open the output file '" + outputFileName + "' for writing." , e ) ; } // Write the object into the output file. try { resultWriter . write ( toWrite . toString ( ) ) ; resultWriter . flush ( ) ; resultWriter . close ( ) ; } catch ( IOException e ) { throw new IllegalStateException ( "There was an error whilst writing to the output file '" + outputFileName + "'." , e ) ; } }
Writes an object using its toString method to the named file . The object may optionally be appended to the file or may overwrite it .