CPSC 124 Introduction to Programming Spring 2007

Lab 8: File and Network IO

Introduction

In this lab, you'll learn how to write programs that read and write files, as well as programs that communicate over the network. This lab will prepare for you the first programming assignment (which is due on 3/29). Both the lab and programming assignments are largely based on a lab by Scotty Orr and Stina Bridgeman. Note: this week's lab was intended to be short to give you added time to work on the programming assignment. Finish the lab as soon as you can, so you can start early on the programming assignment, which will take a significant amount of time. Also, make sure you read the introduction before starting the exercises. It will be crucial for both this lab and the first programming assignment.

Setup

Create a lab08 directory in your cs124 directory to hold the files for this lab. Copy the files from /classes/s07/cs124/labs/lab08/ to your lab08 directory.

File IO

Reread pages 43 and 44 of the book (chapter 2) that cover file IO. If you don't have your book with, the book can be found online at http://math.hws.edu/javanotes5/.

Networking, Servers, and Clients

One of the nice features of Java is that it is relatively easy (at least compared to other programming languages) to write programs which communicate over the network. The details of network communication, however, are beyond what we've covered at this point, so instead you'll be using a class called NetworkTextIO to handle sending and receiving data.

Network programs consist of two parts: a server program which listens for a connection request, and a client program which makes a connection request to the server. The programs can communicate once the connection is established, until the connection is closed. This is very much like using a telephone - the server is the person sitting by the phone waiting for it to ring, and the client is the person who dials the phone on the other end. Once the phone rings and the server picks up the receiver, the two parties can talk. The connection ends when one of the parties hangs up the phone. With phones, the person dialing needs to know the telephone number of the person receiving the call. A similar requirement exists with network programs - the client needs to know the hostname or IP address of the computer where the server program is running. Both parties also need to agree on a port number - you can think of the network connection to a computer as being like a big company which has one phone number and each employee has a different extension. The hostname/IP address is the phone number, and the port number identifies the extension.

A small example of server and client programs can be found in the files NetworkServer.java and NetworkClient.java, which you copied to your lab08 directory. The first thing you do should be to try out these programs. You'll need to cooperate with one of the other students in the lab - one will run NetworkServer and the other will run NetworkClient. (Alternatively, you can open two terminal windows and run one program in each window.) Here is what you should do:

You should now open NetworkServer.java and NetworkClient.java in your favorite text editor so you can see how they work. Both programs use several routines from the NetworkTextIO class. NetworkTextIO is very similar to TextIO. It provides all of the put, putln, get, and getln subroutines that TextIO does, with the difference that "put" now sends the data to the other end of the connection (instead of printing it on the screen) and "get" reads data from the other end of the connection (instead of from the keyboard). As is the case with keyboard input, data which is sent by using "put" on one end of the connection will not be available for reading with "get" on the other end of the connection until a newline is sent, either by explicitly including the special newline character "\n" in the String you give to put or by using putln instead. This is similar to how a program won't process input from the keyboard until you press enter. The simplest thing to do is just to always use putln instead of put.

NetworkTextIO also contains a few additional subroutines needed to setup and manage the network connection to another computer:

Make sure you understand both NetworkServer.java and NetworkClient.java before continuing with the lab.

Exercises

Here are the exercises for this week's lab, due in two weeks (because of spring break). These will help you get started on the first programming assignment, so try to finish the lab as early as possible (it is a shorter lab).

  1. Write a program called CopyFile.java that copies one file to another file. It should prompt the user for a file name to read from and a file name to write to, and then write the contents of the read file to the write file. You will need to use some new methods in TextIO in order to do this: TextIO.readFile(), TextIO.writeFile(), TextIO.eof(), TextIO.putln(). TextIO.readFile and TextIO.writeFile are used to declare that you want to read or write from a specified file, respectively. Note: they do not actually read or write the file. They simply declare that you want to read or write from a specified file (rather than the screen). TextIO.eof is a method that tells you when the end of the read file has been reached (it returns a boolean). TextIO.putln is a method for writing a string to the file specified in TextIO.writeFile. See the book (http://math.hws.edu/javanotes5/) for more details.

    Once you think you have your program working, try copying the provided file 'foo.txt' to 'goo.txt'. Run your program, entering 'foo.txt' for the read file name and 'goo.txt' for the write file name. After the program completes open up the two files in emacs and verify that they are the same.

    Now try entering in a bad file name (a name of a file that doesn't exist) for the read file. What happens? Add a try...catch statement to your program to catch this event. When a bad file name is entered for the read file, your program should print out an error message and then immediately exit the program using the System.exit(1) statement.

    Now try entering the file name ~mcorliss/foo.txt, which you don't have permission to write to. What happens? Add a try...catch statement to your program to catch this event. When a bad file name is entered for the write file, your program should also print out an error message and then immediately exit the program using the System.exit(1) statement.

  2. Modify NetworkClient.java and NetworkServer.java to prompt the user for the message to send to the other (rather than using "foo" in the client and "goo" in the server). Make sure you closely read the "Networks, Servers, and Clients" section so that you understand how the program works. Note: this program will be your starting point for programming assignment 1.

Handin

Verify that your lab08 folder contains all of the files you created or modified for this lab, then copy your entire lab08 folder to the handin directory ~mcorliss/handin/124/username (where username is replaced with your username).