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
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