JamesMadisonUniversity

CS Dept


CS 159 Lab: File IO


Objectives:

Students will:
  • Read data from a file.
  • Write data to a file.

Background:

Much data processing uses files rather than hand entry. Today's lab will use file IO to read data from a file, use that data to create a TextAnalyzer object, and then write the results of the analysis of the data to a new output file.

New Classes:

File - An abstract representation of a File.

PrintWriter - Outputs data. It contains familiar methods like print, println, and printf.

Scanner - While not new, it will be used in a new way.

Materials:

FileIO.java
small.txt
gettysburg.txt

Acknowledgment:

Written by Nancy Harris

1 Setting up the environment for this lab:

  1. Create an Eclipse project for this lab.
  2. Drag the starter file (FileIO.java) to your project.
  3. Drag your TextAnalyzer.java file to your project. (If you do not have a copy from the PA, download your instructor's solution and use it.)
  4. Download the IO files provided and place them at the top level of your project (at the same level as the bin and src directories are located.)

2 First pass, hardcode the file names.

  1. Examine the code provided in FileIO.java. You will see a fully built main and then a series of methods that are called by main. Your job will be to flesh out each of the "stubbed" methods.
  2. Build method getInputScanner. (NOTE: you will not use the Scanner input parameter in this first pass version.)
    1. Instantiate the input File object using "small.txt" for the File name. See the APIs for the File class or Chapter 4.10 (ReadFirstLine.java, pg. 242).
    2. Instantiate the Scanner object for the input file. Put the code that instantiates the Scanner in a try / catch block. In the catch, you should print the error message: "Missing file: " followed by the filename and then exit the program using System.exit(). Use the getName method on the File object to get the filename. Do not just hard code in "small.txt". Why is it better to have the getName provide the name instead of "small.txt"?
  3. Build method getOutputWriter. (NOTE: you will not use the Scanner input parameter in this first pass version.)
    1. Instantiate the output File object using "out.txt" for the File name.
    2. Instantiate the PrintWriter object for the output file. Put the code that instantiates the PrintWriter in a try / catch block. In the catch, you should print the error message: "Missing file: " followed by the filename and then exit the program using System.exit(). Use the getName method on the File object to get the filename. Do not just hard code in "out.txt".
  4. Build method readText.
    1. In this step, you will read lines in from the input file until you reach the EOF condition. Use Scanner's hasNext() method to determine if you have any more input. As you read in each line, you should append them to the String inText.
  5. Method procesText has been written for you to get data about the text that you read in.
    1. In this step, which has been written for you, the TextAnalyzer is instantiated using the text that you have read in and then several of its methods are called with the data stored in a String array..
  6. Method writeText
    1. In this method, you will write out the data that you gathered in the processText method.
  7. Finally, it is a good idea to close your file resources when you are finished with them. As the last step of main, close the Scanners and PrintWriter.

Test your program. Check that the output file looks like this one. out.txt.

3 Second Pass, read the file names from the user.

  1. Save a copy of your working FileIO.java as FileIOV1.java. (That way if you mess something up, you will have a backup copy).
  2. In the first pass, you hardcoded the name of the file into the instantiation of the input and output files. This version will have you prompt the user and ask for the name of a file. Instead of exiting the program if a bad file is given, you will ask the user again for the name of the right file.
  3. Change your getInputScanner method
    1. to prompt the user for a file name and then use the keyboard Scanner to read the result.
    2. "try" to open that file. If the file cannot be opened output a message as before and add in the comment "Try again."
    3. Create a loop around the user prompt and entry and the try catch blocks to continue to prompt the user for a file name until it can be opened.
  4. Change your getOutputWriter method
    1. Prompt the user for a file name and then use the keyboard Scanner to read the result.
    2. "try' to open that file. If the file cannot be opened output a message as before and add in the comment "Try again."
    3. Create a loop around the user prompt and entry and the try catch blocks to continue to prompt the user for a valied output file name.
  5. Test your program using the gettysburg.txt input file and output.txt as the output file.

3 Additional requirements

  1. For sections 2 and 3(A. Harris): You must submit your FileIO.java code to "submit lab51".
  2. For sections 1, 4, and 5 (N. Harris): You must upload your FileIO.java code to Canvas. This is a group assignment so you may upload a single copy for both partners.

Updated 02/09/2014 (nlh)