Question: You will make changes to the class called ReadFile . Complete the progam so that it prints the contents of the file specified by the

You will make changes to the class called ReadFile.

Complete the progam so that it prints the contents of the file specified by the user (code is provided for user input) to System.out.

There are several important requirements:

The main method must not be declared to throw any kind of exception.

The program must not test whether or not the requested file exists before attempting to open it.

The program must use a try/finally block to make sure that the reader or input stream reading the file is closed before the program exits.

There is a file myFile.txt provided that contains the following contents:

Oh freddled gruntbuggly Thy micturations are to me As plurdled gabbleblotchits on a lurgid bee. Groop I implore thee, my foonting turlingdromes And hooptiously drangle me with crinkly bindlewurdles, Or I will rend thee in the gobberwarts with my blurglecruncheon, See if I don't! 

Here is an example session showing a file being printed successfully (user input in bold):

Which file? myFile.txt Oh freddled gruntbuggly Thy micturations are to me As plurdled gabbleblotchits on a lurgid bee. Groop I implore thee, my foonting turlingdromes And hooptiously drangle me with crinkly bindlewurdles, Or I will rend thee in the gobberwarts with my blurglecruncheon, See if I don't! 

Here is another example session showing what happens when the user enters the name of a nonexistent file:

Which file? nonexistent.txt Error: nonexistent.txt (No such file or directory) 

Hints

Use a try/catch block to handle an IOException or FileNotFoundException that might occur. Print the error message from the catch block. You can call the getMessage method on the caught exception object to get a string containing a textual description of the error that caused the exception.

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!