Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is an example of a program that illustrates one way to get data from a file. Notes: a. The expression new BufferedReader( new FileReader(samplefile1.txt))

Below is an example of a program that illustrates one way to get data from a file.

Notes:

a. The expression "new BufferedReader( new FileReader("samplefile1.txt"))" may generate a FileNotFoundException.


b. FileNotFoundException is a Checked Exception.


c. Being a Checked Exception, FileNotFoundException should be handled where it occurs or FileNotFoundException may be propagated through a throws clause(Recall: Unlike, Unchecked Exception, a Checked Exception cannot be ignored) .


d. The sample program shows an example of exception propagation and that the method(i.e. run method) that can generate FileNotFoundException has a throws clause. Should a FileNotFoundException occurs at run time, the FileNotFoundException will be forwarded or propagated to the method that invoked the run method. In this example, the main method invoked the run method. The main method handles the exception(i.e. program.run() is in a try block and the try has a catch block) object that it may receive.


e. FileNotFoundException occurs if the computer discovers that the file being reference (i.e. samplefile1.txt) does not exist in the project workspace. To put the file in the project workspace using IntelliJ, add the file ( Right-click on the project name, choose New, choose File, enter the name of the data file including the extension(samplefile1.txt), type the content of the file.) Explore other ways by which you can add the data file.


Exercise:(It is best to test the program by using a computer)

Test the following program.

I. Run the program even if you did not yet create/add the data file so that you will verify that a FileNotFoundException is generated at run time.


II. Run the program after adding the needed data file. ( Right-click on the project name, choose New, choose File, enter the name of the data file including the extension(samplefile1.txt), type lines of texts as contents of the file. (e.g. line 1 of text then enter, line 2 of text then enter, line 3 of text then enter, and so on. You should not press enter after the last line of text )


1. Show the output of your test(Copy what is in the output screen after your successful test).


*/


package samples;

import java.lang.*;

import java.io.*;

public class TextFileInputDemo1 {

private BufferedReader inputReader;

public void run() throws FileNotFoundException, Exception {

String lineOfText="x";

inputReader = new BufferedReader( new FileReader("samplefile1.txt"));

System.out.println("The line(s) of text in samplefile1.txt is/are: ");

lineOfText= inputReader.readLine();

while ( lineOfText != null){

System.out.println(" " + lineOfText);

lineOfText = inputReader.readLine();

}

inputReader.close();

} // end of run method


public static void main(String[] args){

TextFileInputDemo1 program;

try {

program = new TextFileInputDemo1();

program.run();

} catch ( FileNotFoundException e1 ) {

System.out.println("File samplefile1.txt does not exist.\n" + e1.toString());

} catch (Exception e) {

e.printStackTrace();

}

System.exit(0);

} // end main

} // end class

Step by Step Solution

3.47 Rating (154 Votes )

There are 3 Steps involved in it

Step: 1

Note Exceptions are either handledusing t... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Auditing and Assurance Services

Authors: Timothy Louwers, Robert Ramsay, David Sinason, Jerry Straws

6th edition

978-1259197109, 77632281, 77862341, 1259197107, 9780077632281, 978-0077862343

More Books

Students also viewed these Computer Engineering questions

Question

What is an audit committee? What are its duties?

Answered: 1 week ago