import java.io.*; import java.lang.*; public class WholeFileReader { public String readFile(String path) { try ( FileReader fReader = new FileReader(new File (path)) ) { BufferedReader in = new BufferedReader(fReader); String inputLine; StringBuffer strDocument = new StringBuffer(); while ((inputLine = in.readLine()) != null) { strDocument.append(inputLine); //throw new IOException(); } return strDocument.toString(); } catch(IOException ex) { System.out.println("IOException occured while reading from file "+path); } 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"); } } }