Question: In JAVA, Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result. Use

In JAVA,

Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result. Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox.

Lab 4A: Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. Test with the input file attached.

For example if file input is:

This is a test

File output should be

1. This is a test

Lab 4B Input File.txt

This file contains lines of text to determine if you can properly read and write from a file using exceptions.

Previous Code:

import java.io.*; import java.util.*; public class Module_4B_lab_assignment { public static void main(String[] args) { final String input_file = "Lab_4A_Input.txt"; final String output_file = "Lab_4A_Output.txt"; Scanner x = null; FileWriter y = null; try { x = new Scanner(new File(input_file)); y = new FileWriter(output_file); int count = 1; while (x.hasNextLine()) { String line = String.format("%d. %s ",count++,x.nextLine()); System.out.print(line); y.write(line); } y.flush(); } catch(Exception e) { e.printStackTrace(); } finally { try { y.close(); x.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!