Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

What are our strategic aims?pg 87

Answered: 1 week ago

Question

3. How would you address the problems that make up the situation?

Answered: 1 week ago

Question

2. What recommendations will you make to the city council?

Answered: 1 week ago