Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PurposeDemonstrate your understanding of Java's exception handling and file I / O . Background ThoughtThe term exception is shorthand for the phrase exceptional event.In a

PurposeDemonstrate your understanding of Java's exception handling and file I/O.Background ThoughtThe term exception is shorthand for the phrase "exceptional event."In a nutshell, exceptions are some kind of events (or signals) which occur during program execution and interrupt the regular execution flow. The idea which led to introduction of the exceptions was born as a replacement to the error codes and status checking techniques used back in the days. Since then, exceptions gained widespread acceptance as the standard way to deal with error conditions in many programming languages, including Java.Instructions/Design (10 points TOTAL)We will write a program named JavaByteCode.java to test that all files in our current directory have a .class extension. Any filename that DOES NOT have a .class extension will be logged in an external file.2 points Step 1Create an exception class called NotByteCodeException (inside file named NotByteCodeException.java) designed to be thrown when a filename NOT ending in .class is encountered during processing. 2 points Step 2If a filename is encountered that doesn't fit that description (last 6 characters do not equal ".class"), the exception is thrown.Note: First you want to check If the filename is greater than 6 then grab the last 6 characters using String's substring method storing this substring in its own String variable.Hint: Last character in a String is stringName.length()-12 points Step 3You then create a try/catch block to catch and handle the exception if it is thrown. Handle the exception (inside the catch) by printing the filename (really just a String) to an external file named nonByteCode.txt There is no need for a finally clause.2 points Step 4You will need an outer try/catch to handle the opening of the external file. Also, be careful of scope. Anything declared within try/catch block is local to that block.Hints:Instantiate exception object outside/ before outer try/catch blockClose your external file at bottom of outer try/catch block2 points Step 5Add the code for writing out to a file last. For debugging, have your output go to the console so you can see what is happening within your program.Note: See driver program below to test the exception.Run your program in a directory with some .class files and some non-.class files (example: the file named testit.txt is non-.class file)Warning: Don't forget to close() file so output is flushed to the file.InputAll input comes from the files in the current directory.Here is the onlineGDB project:https://onlinegdb.com/6ImNCZb7H0Here is the code to read files from the current directory "."(represented by a dot):import java.io.*; public class JavaByteCode{ public static void main(String [] args){ File folder = new File("."); //dot is current directory File[] listOfFiles = folder.listFiles(); //we now have array of File objects String name =""; for (File file : listOfFiles){//check it's a file not subdirectory if (file.isFile()){ name = file.getName(); //each file name is a String System.out.println(name); }}}} Sample OutputAll non-.class files will be in the external file nonByteCode.txtAlso, give the user a good-bye message, example: "Program is now finished"Just for illustration... if my directory listing is:TestOverflow.classTestOverflow.javaTestVowel.classTestVowel.javaUntitled.pyMy nonByteCode.txt file will contain:TestOverflow.javaTestVowel.javaUntitled.py

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

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions