CS206
Lab: Friday Feb 4 and Feb 11, 2005
Reading and Writing Files:
The goal of this lab is to get you used to reading from and writing
to files in Java. For Feb 4 you should complete "versions" 1 and 2 as described below.
Reading files:
Create a program that reads the file: /home/gtowell/cs206/cards.
You program should take the name of the file to be read as a command
line argument.
Version 1 of the program should just read the file and write it
line-by-line to the screen.
To read:
put import java.io.*; at the top of your class
for the part of the program that reads the file you will need something
like:
BufferedReader br = new BufferedReader(newFileReader("/home/gtowell/cs206/cards"));
if (br.ready())
br.readLine();
br.close();
You will need more than this (the above is almost sufficient to read
one line of the file) but rather than writing about it here please go
to http://bubo/~gtowell/docs/api then navigate to the section on the
java.io package and then on to BufferedReader.
Once you have completed version 1, move on to version 2. In this
version you will store each line of the file into an array which has
exactly the correct number of spaces in it.
Here down is to be done on Feb 11
Once you have completed version 2, move on to version 3. In this
version, write the array backwards into some file in your directory
structure. As with the input file the name of the file should be
taken from input on the command line.
To write to a file you will need something like:
PrintStream ps = new PrintStream(new FileOutputStream("outfile"));
ps.println("this is a line");
ps.close();
Finally note that the input file has three items on each line.
Adapt version 3 of you program so that you only get the middle item in
the line. You can use the following:
String s = br.readLine();
String[] ss = s.split(" ");
String mid = ss[ss.length/2];
Finally, follow the electronic submission procedure to give me a copy
of your program, the final version only.