Java Reference
In-Depth Information
readBoolean()readByte()
readFloat() readDouble()
readShort () readInt()
readLong()
readChar()
They each return a value of the corresponding type, and they can all throw an
IOException
if an error
occurs or can throw an
EOFException
if the end-of-file is reached. There is also a
read()
method with no
parameter that reads a single byte from the stream and returns it as type
int
. This returns −1 when EOF is
reached and does not throw the exception.
Just to make sure that the process of serializing and deserializing objects is clear, let's try it again in an-
other simple example.
Using Object Serialization
In Chapter 6, you produced an example that created
PolyLine
objects containing
Point
objects in a gener-
alized linked list. This is a good basis for demonstrating how effectively serialization takes care of handling
objects that are members of objects. You can create an example that writes
PolyLine
objects to a file using
serialization.
TRY IT OUT: Serializing a Linked List
The classes
PolyLine
,
Point
, and
LinkedList
and the inner class
ListItem
are the same as in Chapter
6 except that you need to implement the
Serializable
interface in each of them.
The
PolyLine
definition needs to be amended to the following:
import java.io.Serializable;
public final class PolyLine implements Serializable {
// Class definition as before...
private static final long serialVersionUID = 7001L;
}
Directory "TryPolyLineObjects"
The
Point
definition needs a similar change:
import java.io.Serializable;
public class Point implements Serializable {
// Class definition as before...