CPSC 124 (Winter 1998): Lab 10
Files, Networking, and Streams

THE TENTH LAB in Computer Science 124 is meant to be fairly short. After working through the lab, you'll have a print out and the answer to one short question. (You won't know what the question is until you've decoded a secret file that I've posted on the Web!) Turn in the print out and your answer to the question at the end of the lab. (This will be worth 15 points.)

For this lab, you will work with the "Files Starter" folder from the cpsc124 folder on the network. Copy this folder into the java folder on your M drive.

Streams and Exceptions

Chapter 8 of the on-line text covers streams. An input stream is a source of data that can be read by a program. An output stream is a destination to which a program can write data. These two types of streams are represented in Java by the classes InputStream and OuputStream, which are defined in the package java.io. These classes are not generally used directly. Instead, objects belonging to subclasses of InputStream and OuputStream are used. In this lab, you will use the classes DataInputStream and PrintStream.

DataInputStream is a subclass of InputStream. As explained in the text, it is meant for working with binary, as opposed to human-readable, data. However, it does have one method that can be used for human-readable data: the readLine() method. If "in" is a variable of type DataInputStream, then "in.readline()" reads the next full line of data from the input stream, and returns this line as a String. If there is no more data to be read, it returns the value null.

PrintStream is a subclass of OutputStream. It is meant for writing human-readable data. It has methods print() and println() that are used in much the same way as the put() and putln() methods of my Console class.

PrintStreams and DataInputStreams can be created for doing output and input to files. They can also be used for sending and receiving data across the Internet. Streams for performing these different tasks are created in different ways. This is discussed in the text, and you will see examples in the program that you will use in this lab.

In order to work with Streams, you have to know about exceptions, which are covered in Section 8.1 of the text. An exception is thrown when an error or some other unusual event occurs. A program can catch an exception and handle it. There are certain types of exceptions for which error-handling is mandatory. That is, if your program calls a method that can throw the exception, then your program must handle that exception. This is generally done by calling the method in a try statement. In this lab, you'll see several try statements that handle errors that occur during input/output operations. The statements take the form:

          try {
             .   // code that might cause an IOException to be thrown
          catch (IOException e) {
             .   // what should happen if an IOException occurs

Coded Messages and Coded Files

Although this lab is mostly about files and netwoking, I've tried to spice things up with a bit of encryption. Encryption is used to make a message or file unintelligible except to someone who knows the key. We say that the message or file has been encoded and that it must be decoded before it can be read. The "Files Starter" project includes a class, Coder, that can be used to encode and decode data based on a "secret key." The secret key in this case is simply a word or phrase. Data that is encoded using a secret key can be decoded using the same key. (Note: The type of encoding in this example does not give significant protection against attempts to break the code!)

When an object belonging to the class Coder is created, the key word to be used for the encoding is specified in the constructor. The Coder object can then be used to encode and decode Strings using that keyword. The two methods that you will use are:

                String encode(String clearText)
                String decode(String codedText)

See the file Coder.java in the "Files Starter" project for more information. (You can also read it here.)

The program in "Files Starter" will let the user send and receive one-line coded messages over the internet. It will let the user encode entire files. When you are finished with it, it will also let the user decode files and decode coded files downloaded over the World Wide Web. All this illustrates the many ways in which streams can be used in Java.

Run the program in the "Files Starter" project. You will be presented with a menu of choices. Select the option to "Encode a file." You will be asked to specify the name of the source file to be encoded. You can use any file in the "Files Starter" folder. For example, enter Coder.java as the name of the source file.

You will then be asked for the name of the output file. This file will be created, and the coded version of the source file will be written to it. Enter a name, such as Coder.out. (Don't use the name of a file that exists, or it will be overwritten!)

Finally, you will be asked for the key word to be used in the coding process. Enter any word or phrase that you like. (Including spaces or other punctuation is OK, but they will be ignored.)

After the is process complete, you should be able to open the file to see what it looks like after encoding. Use the "Open" command in the Developer Studio "File" menu. Note that you won't be able to decode files until you have implemented the decoding option in the program.

Next, you should try using the options for sending and receiving coded messages over the Internet. You will need to run the program on two computers or cooperate with another group in the lab. To send a message, one group must select the option "Send a coded message over the Internet", and the other must select "Receive a coded message over the Internet." The first group must tell the second what key word they are using to encode the message. The second group must tell the first the Internet address of the computer that will be receiving the message. The computer will print out this information when the "Receive" option is selected.

This example shows that it is fairly easy to use networking in Java. I encourage you to read the subroutines that implement the networking options in this program. They can be found in the file NetworkCodedMessage.java. (You can also read it here.)

Finally, it's time to do some programming. Your first task is to implement the "Decode a file" option. In the menu() subroutine in the main program, change the lines

          case 2:
             console.putln("Sorry, not implemented.");
          case 2:

Then modify the doFile method so that it can handle decoding as well as encoding. You will only have to replace a single line in the program with a few lines. The main point of this exercise is to encourage you to read and to try to understand what is done in the doFile() method. (You can also read the main program here.)

Once you have the program working, use it to decode the file mission.txt. (This file is in the "Files Starter" folder. You can also see it here.) This file was encoded using the keyword "goforit". The decoded file will contain instructions about what you should do for the rest of the lab...

[ Lab Index | Online Notes ]