import java.io.*; import java.lang.*; public class WholeFileReader { public String readFile(String path) throws FileNotFoundException { FileReader fReader = null; try { File file = new File (path); fReader = new FileReader(file); BufferedReader in = new BufferedReader(fReader); String inputLine; StringBuffer strDocument = new StringBuffer(); try { while ((inputLine = in.readLine()) != null) { strDocument.append(inputLine); } } catch(IOException ex) { System.out.println("IOException occured while reading from file "+path); } return strDocument.toString(); } /* //Remove exception from here. Handle it at a higher level catch(FileNotFoundException ex) { System.out.println("The specified file was not found at "+ args[0]); }*/ finally { if( fReader != null) { try { System.out.println("Closing file"); fReader.close(); } catch(IOException ex) { System.out.println("IOException occured while closing file "+path); } } } } public static void main(String args[]) { WholeFileReader wfr = new WholeFileReader(); try { System.out.println(wfr.readFile(args[0]) ); } catch(IndexOutOfBoundsException ex) { System.out.println("No file has been specified!\n"); } catch(FileNotFoundException ex) { System.out.println("The specified file was not found at "+ args[0]); } } }