6.170 Laboratory in Software Engineering
Spring 2001
Lab 3: File I/O

Handout B3

Contents:

When the lab is over, the solutions will be posted.


Purpose

This lab will familiarize you with file I/O in Java. It is not our purpose to explain to you at this time what every class related to file I/O does. Instead, we are going to go over the basic concepts and work on a number of practical exercises that will give you most of the tools you need to work with different types of files, and do other things that you might find useful in your problem sets or while working on your project. After you are finished with this Lab, you should be able to:

Overview of file I/O in Java

Java's I/O system is defined in the java.io package. Thus, whenever you want to work with these classes, you need to import some or all of the files in the package. For instance, you can put the following line in the import section which shall accomplish the task.

import java.io.*;

The basic concept behind Java's I/O is streams. You use streams to either send or receive information in a serial, first in, first out manner. Most of the classes in the java.io package implement some stream. Often you will want to connect one stream to another in order to process data in a certain way. The best place to gain a thorough understanding of the class hierarchy and what the different streams are used for is at Sun's I/O tutorial. Please take a few minutes looking through it, since it covers the basics in a compact manner.

One of the things that you should have gotten out of the Sun tutorial is that there are two different types of streams: ones that deal with 8-bit characters and ones that deal with 16-bit ones. Basically, the 16-bit streams (which usually have a Reader or a Writer at the end of the name) are for processing text and the 8-bit streams are for binary data. Of the latter, two particularly useful sets are the DataInputStream/DataOutputStream and the ObjectInputStream/ObjectOutputStream. You can use the first pair to read/write Java primitive datatypes (int, char, float, etc) and String without having to worry about the details of how to represent boundaries between variables stored in the file. As long as you read the datatypes in the same order that they were written in, Java will use UTF encoding to ensure that the data is restored correctly. UTF is an encoding format that preserves all 16 bits of Java's primitive char type, as opposed to ASCII encoding which only stores 8 bits.

The ObjectInputStream/ObjectOutputStream pair is very important to Java's I/O. Wouldn't it be nice if you could simply issue a few commands and have an object of any class saved to a file and loaded back into your Java program without worrying about the details of how to store the object in a file? Well, Java provides a facility that does exactly that. The Object streams are used to read/write entire objects in a process called serialization. All you need to do to make a class serializable is to declare it as

public class NAME implements java.io.serializable
This is very important, since now no matter how complex the object is that you want to save, you can have persistance with a few relatively simple commands. Once again, Sun's tutorial does a good job of illustrating this, and you should look over that if you hadn't done this already.

Exercises

There are four exercises in this Lab. For all four exercises, you should use the Lab3.java as your main file from which you call all the methods. This file has already been created for you, and it has been made part of the lab3 package which is the package for all the classes in this lab. You should create a lab3 sub-directory inside your 6.170 directory, and copy all the sourcefiles you will need for this lab. The following two commands do this for you:

mkdir ~/6.170/lab3
cp /mit/6.170/www/labs/lab3/* ~/6.170/lab3/
The specs for all the classes that were given to you are provided for your reference. You should refer to them both for the specifications of the classes you are to complete, as well as for the specs of those which have already been done for you.


$Id: lab3.html,v 1.15 2001/02/22 17:32:35 mistere Exp $