Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

We will write a program named JavaOrNot.java to test that all files in our current directory have a .java extension. Any filename that does NOT have a .java extension will be logged in an external fil

We will write a program named JavaOrNot.java to test that all files in our current directory have a .java extension. Any filename that does NOT have a .java extension will be logged in an external file. Step 1: Create an exception class called NotJavaException, designed to be thrown when a filename NOT ending in .java is encountered during processing. If a filename is encountered that doesn't fit that description (last 5 characters do not equal ".java"), the exception is thrown.

Note: First you want to check If the filename is greater than 5  then grab the last 5 characters using String's substring method storing this substring in it's own String variable.

Hint: Last character in a String is stringName.length( ) - 1

Step 2: 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 nonJava.txt  

Note: You will need an outer try/catch to handle the opening of the external file.

Step 3: See driver program below to test the exception.Run your program in a directory with some .java files and some non-.java files (example: testit.txt is non-.java file)

Note: 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.Warning: Don't forget to close() file so output is flushed. NO need for a finally clause.

Input

All input comes from the files in the current directory. Here is the code to read files from the current directory ".":

import java.io.*; public class JavaOrNot { 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); } } } }

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question