Question: Take the following code, ListOfNumbers.java: import java.io.*; import java.util.List; import java.util.ArrayList; public class ListOfNumbers { private List list; private static final int SIZE = 10;
Take the following code, ListOfNumbers.java:
import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class ListOfNumbers {
private List
private static final int SIZE = 10;
public ListOfNumbers () {
list = new ArrayList
for (int i = 0; i < SIZE; i++)
list.add(new Integer(i));
}
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering try statement");
out = new PrintWriter(new FileWriter("outFile.txt"));
for (int i = 0; i < SIZE; i++)
out.println("Value at: " + i + " = " + list.get(i));
} catch (IndexOutOfBoundsException e) {
System.err.println("Caught IndexOutOfBoundsException: " +
e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
}
}
Add a readList method to ListOfNumbers.java. This method should read in int values from a file, print each value, and append them to the end of the ArrayList called list. You should catch all appropriate errors. You will read from the text file numberfile.txt.
The writeList method writes out the contents of the ArrayList to outFile.txt.
Modify the following cat method so that it will compile.
public static void cat(String fileName) {
RandomAccessFile input = null;
String line = null;
try {
input = new RandomAccessFile(file, "r");
while ((line = input.readLine()) != null) {
System.out.println(line);
}
return;
} finally {
if (input != null) {
input.close();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
