File I/O Practice Assignments

Overview

Computer programs that use files can read in information that is external to the program and/or write output. This allows your program to be able to do so much more.

Submission Instructions

Submit each of the following practice assignments at codePost.io. To register for a free codePost account, please follow these instructions.
Watch this short video for a demonstration of submitting an assignment and reviewing the results:
YouTube video: codePost: Submission and Checking Results Thumbnail
Note, currently, feedback is not visible in Safari.

Practice Assignments

FileIO 01: Write a Message

Write a FileIO01 class with a method named writeMessage() to takes message as a String and a filename (as a String) (in that order). Have the method, write the message to the filename. Your code will either need to 1) catch all checked exceptions or 2) use the throws keyword.

public class FileIO01{
    public void writeMessage( String message, String filename ){

    }
}

FileIO 02: Write Array to File

Write a FileIO02 class with a method named writeArrayToFile() that takes an array of Strings and a filename (as a String) (in that order). Write each of the elements in the array to filename, appending a newline character, '\n', onto the end of each line. Note, for the test case to compile, writeArrayToFile() will need to catch any checked exceptions.

FileIO 03: Words in a File

Write a complete Java program in a FileIO03 class that requests a filename from the user and displays each word from that file on a separate line.

Example:
Note, user input is in bold face blue and underlined text is required for test cases.
Please enter a filename to echo: poem.txt
Two
roads
diverged
in
a
wood
and
I
I
took
the
one
less
traveled
by
And
that
has
made
all
the
difference
Hints:

FileIO 04: Echo to ArrayList

Write a FileIO04 class with a method named readFileToArrayList() that takes a filename (as a String) and returns an ArrayList of Strings. For each line in the file, add it as a separate element in the ArrayList. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. If an exception is thrown, display an informative message and return an empty ArrayList.

Examples:
FileIO04 driver1 = new FileIO04();
driver1.readFileToArrayList( "poem.txt" ) // returns (an ArrayList of Strings with 3 elements): {"Two roads diverged in a wood and I", "I took the one less traveled by", "And that has made all the difference"}
FileIO04 driver2 = new FileIO04();
driver2.readFileToArrayList( "bogusFilename.txt" ); // returns an empty ArrayList

FileIO 05: Averages from a File

Write a FileIO05 class with a method named calculateAverage() that takes a filename (as a String). Assume that the file only contains numerical values. Have the method calculate the average of all of the values. Return the average as a Double. If an exception is thrown, display an informative message and return 0.0.

Example:
FileIO05 driver = new FileIO05();
String filename = "numbers0.txt";
Double val = driver.calculateAverage( filename );  // returns 58.3529411764706

filename = "numbers1.txt";
val = driver.calculateAverage( filename );  // returns 62.699999999999996

filename = "numbers2.txt";
val = driver.calculateAverage( filename );  // returns 56.828571428571436