Lab08: File I/O

Objectives

Setup

First, create a lab08 directory:

$ cd cs190m
$ mkdir lab08
$ cd lab08

Then, download Person.java and Lab08.java into the folder you just created.

The Roster Manager

Today's lab involves managing a list of people (a roster). Parts of the roster management have already been written for you; in particular, adding and removing of people (represented by the Person class). However, these rosters are not particularly useful if you cannot write them to more persistent storage than RAM. Your task is to implement saving/loading of rosters to disk.

There are several classes that you'll find useful: Scanner, PrintWriter, and File. You should already be familiar with Scanner; it is often used for reading input from the keyboard. However, in this case, it will be used to read input from a file instead. On the other hand, PrintWriter will be used for writing to a file. In both cases, a File object can be used to initialize objects of the respective types. There are three tasks in total.

createPerson

In the Person class, the createPerson method is left empty. Essentially, this method must reverse the effects of the writeTo method, which is provided for you. For example, a person named "Bob Marley" is saved to file like so:

Person
FirstName Bob
LastName Marley
EndPerson

The createPerson method is given a Scanner object. It should:

  1. Check to see if there is a token left in the input. If not, or the next token is not "Person", return null. If so, assume that there is a complete set of data for another Person object
  2. Read the next token. If the token is "EndPerson", then you can stop reading and create and return the new object. If the token is "FirstName" or "LastName", you should store it in a temporary variable and use it to create the Person object accordingly when the "EndPerson" token is reached.

Note: You cannot assume that the FirstName token will appear before the LastName token; obviously, the writeTo method will always save the first name first, but we want to allow the case that someone edits the file and accidently saves the fields backwards.

Completing The Menu

You'll notice in the Lab08 class that it is simply a large main method. Inside, there's a large switch statement; there are two cases which concern you, which are 5 and 6 (saving and loading). Part of the code for reading and writing is provided for you; however, you must fill in the blanks. That is, you should fill in the declaration for the PrintWriter and Scanner objects, as well as add exception handling and closing of the objects upon completion of the task.

Note: Closing PrintWriter and Scanner objects is not so simple as putting a call to close in the try block. In the event that an exception is thrown, the resource will not necessarily close; what Java language construct allows you to ensure that the resource is always closed?

Testing Your Program

The Lab08.java file can be used for testing your program. The main method is a simple, command-line menu, which should be self-explanatory. Sample output is provided below:

1.  Add a person to the roster
2.  Remove a person from the roster
3.  Clear the roster
4.  Print the roster
5.  Save the roster
6.  Load a roster
7.  Quit

Enter your choice: 1
Creating a new user...

Enter the first name: Bob
Enter the last name: Marley

1.  Add a person to the roster
2.  Remove a person from the roster
3.  Clear the roster
4.  Print the roster
5.  Save the roster
6.  Load a roster
7.  Quit

Enter your choice: 4
0.  Person:  Name-Bob Marley

1.  Add a person to the roster
2.  Remove a person from the roster
3.  Clear the roster
4.  Print the roster
5.  Save the roster
6.  Load a roster
7.  Quit

Enter your choice: 5
Enter a file to save to: test.txt

1.  Add a person to the roster
2.  Remove a person from the roster
3.  Clear the roster
4.  Print the roster
5.  Save the roster
6.  Load a roster
7.  Quit

Enter your choice: 3
Roster cleared.

1.  Add a person to the roster
2.  Remove a person from the roster
3.  Clear the roster
4.  Print the roster
5.  Save the roster
6.  Load a roster
7.  Quit

Enter your choice: 4

1.  Add a person to the roster
2.  Remove a person from the roster
3.  Clear the roster
4.  Print the roster
5.  Save the roster
6.  Load a roster
7.  Quit

Enter your choice: 6
Enter a roster to load: test.txt
1 entries loaded.

1.  Add a person to the roster
2.  Remove a person from the roster
3.  Clear the roster
4.  Print the roster
5.  Save the roster
6.  Load a roster
7.  Quit

Enter your choice: 4
0.  Person:  Name-Bob Marley

1.  Add a person to the roster
2.  Remove a person from the roster
3.  Clear the roster
4.  Print the roster
5.  Save the roster
6.  Load a roster
7.  Quit

Enter your choice: 7
Goodbye!

Turning In Your Lab

To turn in your lab:

$ cd ~/cs190m/lab08
$ turnin -v -c cs190m -p lab08 *.java

Getting Help

Many of you will probably run across problems while programming at some point during a lab. If that's the case, here are the resources you should use, in order:

  1. Your textbook
  2. Lecture notes
  3. Java API documentation
  4. Your lab TA

Lab created by: Daniel Tang