Colorado State University

Recitation R15 - File Input & Output
Fall 2013

CS160: Foundations in Programming


The purpose of this lab is to:

Phase 1

Make a new project in Eclipse called R15, with a main method. Download the data file from here. Set up the Run Configurations so that R15 has the program arguments:

input.txt output.txt

This is the same as reading command line arguments from the terminal. Create two methods with the following signatures:

static void readFile( String inputFile ){ }

static void writeFile( String outputFile ){ }

Within the main method, call the readFile method with the first command line argument (args[0]), then call the writeFile method with the second command line argument (args[1]).

Phase 2

Implement the readFile method to open the input file, print the type of the next token and the token itself, and then close the file when finished. Exceptions should be handled as shown in class and in the recitation. Make sure your program successfully reads the input values and prints them. Remember that some types can be upgraded from a lower type to higher type. The output from the provided file (input.txt) should exactly match the following, use a formatter to make sure there is always one digit before the decimal point and two digits after a decimal point:
String: It's
Boolean: true
String: that
String: in
Integer: 1492
String: Columbus
String: sailed
String: the
String: ocean
String: blue.
String: 5um
String: num13er5
String: h33r
Double: 3.14
Integer: 6
Double: 1.62
Double: 5.00
Double: 0.00

Phase 3

Implement the writeFile method to open the output file, read input from the user via the keyboard, print the input to the file, and closs the output file. When the user types stop by itself on its own line, the method should terminate.


Given this input from the console:

Hello! Here are some words.
stop can be written here, but it still makes it in.
so will this stop.
stop!
stop

The output file should exactly match the following:

Hello! Here are some words.
stop can be written here, but it still makes it in.
so will this stop.
stop!


Show your R15.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2013 CS160 Colorado State University. All Rights Reserved.