Colorado State University

Recitation R15 - File Input & Output
Spring 2015

CS160: Foundations in Programming


The purpose of this lab is to:

Phase 0

Make a new project and class in Eclipse called R15, with a main method. Set up the Run Configuration so that R15 has the program arguments:

input.txt output.txt

This is the same as reading command line arguments from the terminal.

Here's some code that provides you some examples. It's not meant to be run as a program. You can add this code to your R15 somewhere and comment it out so you have it as a reference:


// Example of reading a file
try {
	// make sure TA explains this line and the try/catch block
	Scanner fileReader = new Scanner(new File(args[0]));
	
	// Examples: (not meant to be run as a program)
	String example1a = fileReader.next();
	String example1b = fileReader.nextLine();
	double example2 = fileReader.nextDouble();
	int example3 = fileReader.nextInt();
	char example4 = fileReader.next().charAt(0);
	
	if (fileReader.hasNext()) {
		// I can grab a string token safely
		String example5 = fileReader.next();
	}
	
	if (fileReader.hasNextDouble()) {
		// I can grab a double safely
		double example6 = fileReader.nextDouble();
	}
	
	if (fileReader.hasNextInt()) {
		// I can grab a int safely
		int example7 = fileReader.nextInt();
	}
	
	fileReader.close();
	
} catch (FileNotFoundException e) {
	System.out.println("ERROR!");
	System.exit(0);
}


// Example of writing to a file
try {
	// make sure TA explains this line and the try/catch block
	PrintWriter fileOutput = new PrintWriter(new File(args[1]));
	
	// Examples:
	fileOutput.println("Hey...");
	fileOutput.print("I've seen...");
	fileOutput.print("this stuff before.\n");
	fileOutput.printf("%.3f\n", 3.456789);
	
	// Important! Save the file
	fileOutput.close();
	
} catch (FileNotFoundException e) {
	System.out.println("ERROR!");
	System.exit(0);
}

The TA will explain the lines that create the Scanner and PrintWriter and why we need a try-catch (and what that is).


Phase 1

Create two methods with the following signatures:

public static void readFile( String inputFile ){ }

public 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 writeFile method to open the output file, prompt the user with "Enter text:", then read input from the user via the keyboard, print the input to the file, and close the output file. When the user types stop (uppercase or lowercase) by itself on its own line, the method should terminate.

Given this input from the console:

Enter text:
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!


Phase 3

  1. Create methods with the following signatures:

    public static double computeAreaCircle( double radius ){ }

    public static double computeAreaRectangle( double height, double width ){ }

    public static double computeAreaTriangle( double height, double base ){ }


  2. Implement all of the methods shown above. When in need of pi, it is acceptable to use Math.PI
    Test each method using your own data values.



Phase 4

Now design a file format that specifies a keyword for a shape on each line, followed by the value(s) needed for that shape. The file format must allow a user to define any number of the following shapes in any order: Implement the readFile method such that it can read and parse the file, discarding the keywords and calling all appropriate methods for that shape. The keywords in the input file are chosen by you. Next print the results of each computation to the console, including the shape name, method name, input values, and calculated value. Make a data file that has several shapes, with the types intermixed, for testing.
Show your R15.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2015 CS160 Colorado State University. All Rights Reserved.