Question: This method to print disk files that have String content has a problem: public void printTextFile(String fileName) throws IOException { BufferedReader diskFile = new BufferedReader(

This method to print disk files that have String content has a problem: public void printTextFile(String fileName) throws IOException { BufferedReader diskFile = new BufferedReader( new FileReader(fileName)); while(true) { System.out.println(diskFile.readLine().trim()); } } If the input file named by the parameter is not found, FileNotFoundException (a child class of IOException) is thrown back to the caller as expected. The programmer also anticipates that EOFException (another child class of IOException) will be thrown when EOF (End-Of-File) is reached. But the method above always blows up with a NullPointerException! Show simple alternate code ***for the while() loop*** that would accomplish file reading, triming, and printing without blowing up. (P.S. Do not throw Exceptions other than IOException back to the caller.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!