Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Main topics : Checked Exceptions Files I/O Exercise This week we will be practicing using Checked exceptions and basic file I/O. Getting Started To

JAVA

Main topics:

Checked Exceptions

Files I/O Exercise

This week we will be practicing using Checked exceptions and basic file I/O.

Getting Started

To start this exercise, you should:

1. Open eclipse and start a new Java project named Lab10

2. Add a Class (named Files) to this project, and copy the contents of the Files.java file provided into it.

Requirements

Files.java

A very simple driver class which you can use as a base. Currently the program asks its user for the name of a text file which will be created; have the contents of the array words copied into it; and then be closed. Notice that since the PrintWriter constructor call may throw a FileNotFoundException, which is a checked exception, we must use try - catch blocks appropriately here.

Your job is to:

1. add the necessary components to the program so that the the original text file will be re-opened - again for output writing; have the contents of the arraymoreWords appended to it; and then closed again.

2. add the necessary components to the program so that the the text file will be re-opened one last time - this time for input reading; have its contents read (line by line) and printed to the screen; and then closed for the last time.

Your finished program will run / produce output similar to:

Enter the file name for output: test.txt

Enter the file name to append to: test.txt

Enter the file name for input: test.txt

words[0] = hello

words[1] = world

words[2] = and

words[3] = stuff

moreWords[0] = the

moreWords[1] = end

-------------------------------------------------------------

Files Code

import java.util.Scanner; import java.io.*;

public class Files { public static void main(String[] args) { String[] words = {"hello", "world", "and", "stuff"}; String[] moreWords = {"the", "end"}; Scanner stdIn = new Scanner(System.in); PrintWriter fileOut; String fileOutName; try { System.out.print("Enter the file name for output: "); fileOutName = stdIn.next(); fileOut = new PrintWriter(fileOutName); for (int i = 0; i < words.length; ++i) fileOut.println("words[" + i + "] = " + words[i]); fileOut.close(); } catch (FileNotFoundException e) { System.out.println("File Error : " + e.getMessage()); } } }

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

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

Students also viewed these Databases questions

Question

=+What is the nature of the plant or site-level role of unions?

Answered: 1 week ago