question: private void writeDocument ( File outputDir , Document xmlDoc , Transformer transformer , String name , String currentDateString ) throws TransformerException , IOException { DOMSource source = new DOMSource ( xmlDoc ) ; File outputFile = new File ( outputDir , "workflow.xml" ) ; StreamResult result = new StreamResult ( outputFile ) ; transformer . transform ( source , result ) ; BufferedReader reader = new BufferedReader ( new FileReader ( outputFile ) ) ; List < String > lines = new ArrayList < > ( ) ; String line ; while ( ( line = reader . readLine ( ) ) != null ) { lines . add ( line ) ; } reader . close ( ) ; BufferedWriter writer = new BufferedWriter ( new FileWriter ( outputFile ) ) ; for ( int i = 0 ; i < lines . size ( ) ; i ++ ) { line = lines . get ( i ) ; if ( i == 1 ) { writer . write ( String . format ( "<!-- %s workflow autogenerated by Arbiter on %s -->\n" , name , currentDateString ) ) ; } writer . write ( line + "\n" ) ; } writer . close ( ) ; }
target:  Write an XML document to a file