Answered step by step
Verified Expert Solution
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 IOBackground 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.InstructionsDesign points TOTALWe 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 points Step Create 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. points Step If a filename is encountered that doesn't fit that description last characters do not equal class" the exception is thrown.Note: First you want to check If the filename is greater than then grab the last characters using String's substring method storing this substring in its own String variable.Hint: Last character in a String is stringName.length points Step You then create a trycatch 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 points Step You will need an outer trycatch to handle the opening of the external file. Also, be careful of scope. Anything declared within trycatch block is local to that block.Hints:Instantiate exception object outside before outer trycatch blockClose your external file at bottom of outer trycatch block points Step Add 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 nonclass files example: the file named testit.txt is nonclass fileWarning: 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:onlinegdbcomImNCZbHHere is the code to read files from the current directory represented by a dot:import java.io; public class JavaByteCode public static void mainString 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 : listOfFilescheck it's a file not subdirectory if fileisFile name file.getName; each file name is a String System.out.printlnname; Sample OutputAll nonclass files will be in the external file nonByteCode.txtAlso, give the user a goodbye 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started