Lab 22: Reading and Writing Files


Due Monday, May 14 at 11:59 PM

Goals for This Lab

By the time you have completed this work, you should be able to:

Provided files:

Step-by-Step Instructions

Step 1: Download and Edit PrintEveryOtherLine.java

This program will open a given file for reading, specified on the command-line. It will then print out every other line of the file, printing the first line of the file.

For example, if you first download colors.txt, and then run your program with the command-line argument colors.txt, you should get the following output:

1: Red
3: Green
5: Orange
7: Black
9: Pink

The comments in the program provide more details.

Step 2: Download and Edit WriteArgsToFile.java

This program will take some command-line arguments, and write them to a new file named “args.txt”, one per line. For example, if you run your program with the command-line arguments foo bar baz, it should produce a file named args.txt with the following contents:

foo
bar
baz

The comments in WriteArgsToFile.java provide further details.

Note that the code you write in writeArgsToFile should write to whatever file is passed to writeArgsToFile. In the given program, this is always args.txt, provided by main. However, this could be any arbitrary file, and I will test your code with alternative File objects.

Step 3: Turn in Your Solution Using Canvas

Log into Canvas, and go to the COMP 110L class. Click “Assignments” on the left pane, then click “Lab 22”. From here, you can upload your code. Specifically, you must turn in the following two files:

In addition, if you collaborated with anyone else, be sure to download collaborators.txt and write the names of the people you collaborated with in the file, one per line. Please submit this file along with the other two files.

You can turn in the assignment multiple times, but only the last version you submitted will be graded.