package utilities; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; /** * Class to handle file management * * @author Rukmal Fernando * @author Hardik Kotecha * @author Radhika Srinivasan */ public class FileManager { private static FileManager reference; /** * default constructor */ private FileManager() { reference = null; } /** * Initialize a new XMLFileWriter if one does not exist * @return InputReader */ public static FileManager initialize() { if ( reference == null ) return new FileManager(); else return reference; } /** * was text pText successfully written to the File at pPath * * @param pPath : String * @param pText : String * @return boolean */ public boolean writeToFile( String pPath, String pText ) { try { boolean exists = ( new File( pPath ) ).exists(); if ( exists ) { BufferedWriter out = new BufferedWriter( new FileWriter( pPath ) ); out.write( pText ); out.close(); } return true; } catch ( IOException e ) { e.printStackTrace(); return false; } } /** * create a file given a path and return success of operation * * @param pPath : String * @return boolean */ public boolean createFile( String pPath ) { try { File givenFile = new File( pPath ); givenFile.createNewFile(); return true; } catch ( IOException e ) { e.printStackTrace(); return false; } } /** * delete a file at the given path and return success of operation * * @param pPath : String * @return boolean */ public boolean deleteFile( String pPath ) { File givenFile = new File( pPath ); boolean deleted = false; if ( givenFile.exists() ) { deleted = givenFile.delete(); if ( !deleted ) throw new RuntimeException( "failed to delete " + pPath ); } return deleted; } /** * does a file exist at this path? * * @param pFilePath : String * @return boolean */ public static boolean exists( String pFilePath ) { boolean exists = ( new File( pFilePath ) ).exists(); return ( exists ); } }