Java Reference
In-Depth Information
Reading Basic Data from an Object Stream
The ObjectInputStream class defines methods for reading basic types of data back from an object
stream. They are:
readBoolean()
readByte()
readChar()
readShort()
readInt()
readLong()
readFloat()
readDouble()
They each return a value of the corresponding type and they can all throw an IOException if an error
occurs, or an EOFException if the end-of-file is reached.
Just to make sure that the process of serializing and deserializing objects is clear, we will use it in a
simple example.
Using Object Serialization
Back in Chapter 6, we produced an example that created PolyLine objects containing Point objects
in a generalized linked list. This is a good basis for demonstrating how effectively serialization takes care
of handling objects that are members of objects. We can just modify the class TryPolyLine to
use serialization.
Try It Out - Serializing a Linked List
The classes PolyLine , Point , and LinkedList and the inner class ListItem are exactly the same
as in Chapter 6 except that we need to implement the Serializable interface in each of them.
The PolyLine definition needs to be amended to:
import java.io.Serializable;
public final class PolyLine implements Serializable {
// Class definition as before...
}
The Point definition needs a similar change:
import java.io.Serializable;
public class Point implements Serializable {
// Class definition as before...
}
The LinkedList class and its inner class likewise:
import java.io.Serializable;
public class LinkedList implements Serializable {
// Class definition as before...
Search WWH ::




Custom Search