Question: 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
------------------------------------------------------------
What I have so far
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();
//append with Filewriter
System.out.print("Enter the file name to append to: ");
fileOutName = stdIn.next();
PrintWriter fileOut1 = new PrintWriter(new FileWriter(fileOutName,true));
for (int j = 0; j < moreWords.length; ++j)
fileOut.println("moreWords[" + j + "] = " + moreWords[j]);
//after appending, use scanner to read file
System.out.print("Enter the file name for input: ");
fileOutName = stdIn.next();
}
catch (IOException e)
{
System.out.println("File Error : " + e.getMessage());
}
}
}
-------------------------------------------------------------
Files Code that was first given
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
Get step-by-step solutions from verified subject matter experts
