Java Input/Output  «Prev   Next»

Reading Writing Java - Exercise

Implement methods

You will notice some methods are displayed in bold in the source code you just copied. These methods are currently unimplemented. Each of these methods does something related to streams and file I/O. In this module, you will implement each of the five unimplemented methods to make the applet save and restore its drawn shapes. Since you do not have to handle the exceptions in them (do you see why?), you should be able to write each method in three lines of code r less (a number of them can be written in just a single line).

Draw methods

There are five methods in the Draw class, which define the applet. These five methods are:

DataOutputStream createOutputStream() 
 throws IOException {
}
DataInputStream createInputStream() 
 throws IOException {
}
void writeNumShapes(DataOutputStream out) 
 throws IOException {    
}
int readNumShapes(DataInputStream in) 
 throws IOException { 
}
int readShapeType(DataInputStream in) 
 throws IOException {  
}

Implementing the Draw methods


We have named these methods as descriptively as possible.
  1. createOutputStream() creates a new DataOutputStream that uses a FileOutputStream as its target. To create the FileOutputStream, you will need to create a File object. The File object should be created using the name of the file you would like to write to and read from (such as a file named shapes).
  2. createInputStream() creates a new DataInputStream that uses a FileInputStream as its target. To create the FileInputStream, you will need to create a File object. The File object should be created using the name of the file you would like to write to and read from.
  3. writeNumShapes() writes the number of shapes in the Vector named drawnShapes to the stream passed to it. You can find the size of the Vector by invoking the Vector's size() method.
  4. readNumShapes() reads from the stream passed to it and returns an int type.
  5. readShapeType() reads from the stream passed to it and also returns an int type.
If you have any questions about the role of these methods, look over the source code and see how they are used. Also, look over the classes in java.io for more information regarding methods and classes to use.

Shape methods

There are also two methods in the Shape class that you must complete. These can each be written in a single line.
void writeIntToStream(DataOutputStream out, int i) throws IOException {
}
int readIntFromStream(DataInputStream in) throws IOException {
}

The purpose of these two methods is fairly clear.
  1. writeIntToStream() writes an int value to a file using a DataOutputStream object.
  2. readIntFromStream() reads and returns an int value from a file using a DataInputStream object.
Once you implement these methods, you can compile and run your applet.When you do, you will find that, if everything works, you can save your drawing to a file named shapes, and restore your drawing from this file as well.
Be sure to look over the code we have supplied to see how everything fits together. Notice that we are writing out the instance data for each drawn shape and reading it all back in, once we have established which shape we are reading.
Once it is all working smoothly, copy and paste your code for these seven methods into the text area below.