Lab 19: File I/O

The goal of this lab is to practice file I/O and to review designing windows.

1. Introduction

In this lab we will revisit the Sprite program from lab 17. This time we will give the program the facility to save the current state of the simulation and reload it later.

The program should work as before but with two changes:

For your convenience, the GUI and File I/O handouts from class are available online.

2. Set up

Follow these set-up instructions carefully.

Make a directory for this lab and cd into it. First, copy files from the course directory for this lab:

hg clone /cslab/class/csci235/lab19
cd lab19

Then copy from your lab17 directory AddListener.java and any other class that you wrote (you should have written another action listener, perhaps named something like OkListener.java. If you previously modified either Sprite.java or Sprites.java, talk to the instructor about how to make your version work with this lab.

If you did not do the Sprite lab, copy the files from /cslab/class/csci235/lab19b/.

However you got the listener class files, you probably want to add them to local Mercurial repository and commit them before you go further.

Remember that you will need to include -cp simulation.jar:. with both the javac and java commands.

3. Designing a protocol

First, discuss with your partner how you can save/store a collection of Sprites. You'll need to decided on a file format/protocol.

4. Writing the SaveListener

Next, finish the SaveListener class so that its actionPerformed() method puts up a window asking for a filename. (The simplest way to do this will be to provide a JTextField in which the user can type the filename. Keep it simple for now.) This window should also have a button labelled "Ok" or the equivalent.

Then write another action listener class which will close that window and do the actual saving. Be sure to catch error cases. It is OK if you print any error messages to the terminal.

5. Loading a saved configuration

Finally, in the file Sprites.java, find the portion of code that will load a configuration from a file and fill-in what's missing.

6. Turn in

Turn in your source files as lab19 with the command

/cslab/class/csci235/bin/handin lab19 *.java

7. Using dialogs (optional)

If you have time, here are instructions for using dialog classes to select a file and to display messages. If you want to try that, be sure that you first commit your changes to your repository before you start. Be sure you turn in what have done before you go on.


Thomas VanDrunen, Cary Gray
Last modified: Fri Dec 2 12:03:27 CST 2011