Colorado State University

Recitation R9 - File Input & Output
Summer 2016

CS160: Foundations in Programming


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);
}

© 2015 CS160 Colorado State University. All Rights Reserved.