Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given a java source file: MyFileReader.java, which reads the contents of a given file based on the file name and returns the contents

You are given a java source file: MyFileReader.java, which reads the contents of a given file based on the file name and returns the contents as a String object. You are also given two text files, TextFile1.txt and TextFile2.txt. Finish the following problems based on this file.

Create a customized exception class WrongFileException, in this class, override the getMessage method so that this exception class always returns the same exception string.

Create a class Homework05 that utilizes the given MyFileReader class to read the text files, display the contents of the text file on the screen.

In Homework05 class, create a try block. In the try block, if the file content equals Right file, display a message indicating You chose the right file! Otherwise, throws a WrongFileException object.

In Homework05 class, create a catch block. In the catch block, display the exception classs message.

import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class MyFileReader { /* * To read a file, call the static readFile method of this class, and it will open it, get its contents, and return the String. */ public static String readFile(String filename) throws IOException { FileReader fr = null; BufferedReader br = null; StringBuffer sb = new StringBuffer(); try { fr = new FileReader(filename); br = new BufferedReader(fr); String s; while((s = br.readLine()) != null) { sb.append(s); } } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } finally { if (fr != null) { fr.close(); } if (br != null) { br.close(); } } return sb.toString(); } } 

text file one is just "Right File"

text file two is just "Wrong File"

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

More Books

Students also viewed these Databases questions

Question

Which form of proof do you find most persuasive? Why?

Answered: 1 week ago