JamesMadisonUniversity

CS Dept

CS-159: FILE I/O Lab

 

Objectives:

To gain practice in reading data from a file.
To gain practice in writing data to a file.

To catch multiple exceptions from a single statement.
To gain practice parsing input.

Background:

In this application, you will practice input and output to a file.  In this case, you are writing a preprocessor which will run through a file and create a new file that has only those lines from the file that meet the criteria for the file.

The lines of the file should be of the form of 3 integers separated by commas.  Each line will end with a new line character.  

If the line contains 3 valid integers, you will write the entire liine out to an output file by the name of userNamegood.txt.  

If the line contains anything other than 3 valid integers, you should display the entire line and write the line to a file userNamebad.txt.

Terms:

Delimiter - A symbol used to separate tokens in an input stream.
Token - One unit of data.

Charset - A set of characters to use in interpreting file characters.See Charset class.

Materials:

Testing file: tester.txt

Stub: FilePreprocess.java

 

Acknowledgment

1 Setting up your environment:

  1. Create your development environment (use a new folder for this lab).
  2. Download the java source files FilePreprocess.java.
  3. Look at the code in FilePreprocess.java. The main method will call the other methods for the preprocessing to work and this has been written for you.
  4. The overall process is this (as you see it in the main method):
    1. You will prompt the user for an input file.
      1. While that file cannot be read, continue to prompt the user.
    2. You will prompt the user for an output file.
      1. While that file cannot be written to, to continue to prompt the user.
    3. Read in the contents of the input file, line by line.
      1. If the line is not of the correct format, print it to error input, System.err.
      2. If the line is of the correct format, print it to the output file.

2 Reading and writing to a file - Phase I:

  1. Create the getName method. See the documentation for what this method should do. You will use this for the input file, encoding and output file names.
  2. Create the getFileScanner method. Notice that it takes in two parameters. The name of the file as a String and the name of the encoding for the file (in our case, "UTF-8"). See the documentation for how to build the getFileScanner method including error handling.
  3. Create the getPrintWriter method. The documentation contains information about how to build the method including error handling.
  4. Create the readData method. This method takes in the file Scanner and the PrintWriter. It reads in each line from the input file and outputs only good lines to the output file. It should call the checkLine method (which has been written for you) for each line to determine if a line is in the appropriate configuration (3 integers separated by commas). If a line is not of the correct format, it should be written to System.err (like System.out, but for error output). If it is in the correct format output it to the "good file".

3 Reading and writing to two files - Phase II:

Save your Phase I file as FileProcessV1.java.

Instead of printing the error lines to the error output, you should send it to an error file.

  1. In main, add in another PrintWriter and just like you did for the good PrintWriter, prompt the user for the name of the file and open the file for writing.
  2. Create an overloaded readData method that will take in three parameters, the input Scanner and two output PrintWriters, one that is for good input and one that is for bad input.
  3. The overloaded readData method should print the bad line to the error file.

4 Getting credit:

See your instructor for how you will receive credit for this assignment.

Updated 02/13/2014 (nlh/jah)