package output; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; /** * Class to handle writing files * * @author Rukmal Fernando * @author Hardik Kotecha * @author Radhika Srinivasan */ public class XMLFileWriter { private static XMLFileWriter reference; /** * default constructor */ private XMLFileWriter() { reference = null; } /** * Initialize a new XMLFileWriter if one does not exist * @return InputReader */ public static XMLFileWriter initialize() { if( reference == null ) return new XMLFileWriter(); else return reference; } /** * did the write successfully write the text pText to the File at pPath * * @param pPath * @param pText * @return boolean */ public boolean writeFile( String pPath, String pText ) { try { boolean exists = ( new File( pPath ) ).exists(); //find out if the File exists if ( exists ) //if the File already exists { boolean deleted = ( new File( pPath ) ).delete(); //delete the File if ( !deleted ) { throw new IOException( "deletion of xml failed" ); } } File xmlFile = new File( pPath ); //create a new File boolean success = xmlFile.createNewFile(); //check that the File was created if ( !success ) //if the create failed { throw new IOException( "creation of xml failed" ); //throw an exception } FileWriter fw = new FileWriter( pPath ); BufferedWriter out = new BufferedWriter( fw );//create a write to the File out.write( /*xmlConst +*/ pText ); //write to the File out.flush(); out.close(); //close writer fw.close(); System.out.println("Closing file " + pPath); } catch( IOException e ) { e.printStackTrace(); } return true; //write completed successfully } }