import java.io.*; import java.lang.*; public class WholeFileReader { public String readFile(String path) { FileReader fReader = null; try { File file = new File (path); fReader = new FileReader(file); BufferedReader in = new BufferedReader(fReader); String inputLine; StringBuffer strDocument = new StringBuffer(); while ((inputLine = in.readLine()) != null) { strDocument.append(inputLine); } return strDocument.toString(); } catch(FileNotFoundException ex) { System.out.println("The specified file was not found!"); return ""; } catch(IOException ex) { System.out.println("IOException occured while reading from file "+path); } finally { if(fReader!=null) { try{ System.out.println("Closing file!"); fReader.close(); } catch(IOException ex) { System.out.println("IOException occured while closing file "+path); } } else { System.out.println("File already closed!"); } } return ""; } 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 from command line!\n"); } } }