CS-239: FILE I/O Lab

JamesMadisonUniversity

Objectives:

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

Background:

In this application, you will practice input and output to a file.  In this case, you will will read lines from an input file.  The lines 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 should write the entire liine out to an output file by the name of good-out.txt.  If the line contains anything other than 3 valid integers, you should display the entire line and write the line to a file bad-out.txt.

Terms:

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

Materials:

Testing file: tester.txt

Part 1: FilePlay2.java, Util1.java

Part 3: part3-out.txt

Part 4: part4-out.txt

Acknowledgment

1 Setting up your environment:

  1. Create your development environment (use a new folder for this lab).
  2. Download the java source files FilePlay2.java, and Util1.java.
  3. Look at the code in FilePlay2.java. Notice how the input stream is opened using a method that returns null if an error occurs, no exception is thrown. The output stream is opened using the traditional PrintWriter method so a try/catch block is needed to handle exception. Which style do you prefer?
  4. Look at the code in Util1.java which implements newScanner and newPrintWriter. These utilities are examples of a "tool" a programmer makes for themselves to simplify writing code in future programs. ArgsChecker is another example of this kind of utility.

2 Reading and writing to a file:

  1. Add code to FilePlay2.java to have a loop such that the body of the loop reads a line from the input file, stores it in a String variable called line, and outputs the line to both System.out and the output file.
  2. Your loop should terminate when you reach the end of the file.
  3. After your loop, use the flush() method flush the buffered output to the outfile.
  4. Test your code using tester.txt.  You should see each line on the screen and you should also see each line in good-out.txt.

3 Parsing the input line:

Scanners can operate on strings as well as operating on System.in and on files. Scanners can also be used to split what they are scanning (string, file, keyboard) into tokens.  In previous program, you have used the "parsing" capability of scanners to read in individual integers or other values from the keyboard.  The default delimiter for the Scanner objects is whitespace (new line, tab, space, etc).  But in this lab, the integers are separated by commas.  In this part of the lab, we are going to use a scanner on line and have it "parse" the line for tokens delimited by the comma instead of the white space.
Note: Building a Scanner over a String does not change the original String.  You can use the original line to write to the display and write to the output files.
  1. Copy FilePlay2.java into FilePlay3.java and rename the class.
  2. In the loop, remove the code that outputs each line read to System.out (keep the line that outputs to the output file).
  3. In the loop, after you have read in the line, open the Scanner so it scans the line that was input. You may use either the try/catch block mechanism or newScanner.
  4. Call the method useDelimiter(",") (pass the comma symbol into the method) after opening the scanner.
  5. Create an inner loop to scan the line for tokens. Count the tokens found and, output each token to System.out using the format "Token:
    %s\n"
    .
  6. When you have exhausted the tokens on the line, output the count of tokens for each line to System.out using the format "Count:
    %d for line \"%s\".\n"
    . Don't worry at this point about how many we have.
  7. Test your code using tester.txt.  You should see each line in good-out.txt. Your screen output should look like this: part3-out.txt.

4 Splitting the line:

In this part of the lab, you will change your program to check that each line has exactly three tokens and each of these must be an integer.  If any token in the line is not an integer, or if there are too many or too few tokens, then the program must not write the line to good-out.txt, but instead should output the line to System.out and output it to bad-out.txt.
  1. Copy FilePlay3.java into FilePlay4.java and rename the class.
  2. Alter the loop that you created in step e in part 3.  In this version, you should check each token to make sure that it is an integer. Use hasNextInt() instead of hasNext() and nextInt() instead of next().
  3. Exit the loop if there is not an integer or if the counter reaches 4.
  4. If the line does not have exactly 3 integers, output the line to both System.out and bad-out.txt.
  5. If the line has exactly 3 integer tokens, then write the line to the good-out.txt file.
  6. Test your code using tester.txt.  You should see each valid line in good-out.txt. Your screen output and bad-out.txt should look like this: part4-out.txt.

5 Not required but useful exercise:

Create an InputParser class that does the work of parsing the input line.
  1. Copy FilePlay4.java into FilePlay5.java and rename the class.
  2. Download InputParser.java.
  3. Copy the code that parses a line from FilePlay5.java into the constructor for InputParser. Adjust as needed so the constructor parses the line.
  4. Replace the code the parses the line in FilePlay5.java with calls to the InputParser constructor and isValid methods.

6 Getting credit:

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

Updated 02/03/2009 (nlh/jah)