Java Reference
In-Depth Information
Interfaces ObjectOutput and ObjectInput
The ObjectOutput interface contains method writeObject , which takes an Object as an
argument and writes its information to an OutputStream . A class that implements inter-
face ObjectOutput (such as ObjectOutputStream ) declares this method and ensures that
the object being output implements interface Serializable (discussed shortly). Similarly,
the ObjectInput interface contains method readObject , which reads and returns a refer-
ence to an Object from an InputStream . After an object has been read, its reference can
be cast to the object's actual type. As you'll see in Chapter 28, applications that commu-
nicate via a network, such as the Internet, can also transmit objects across the network.
15.5.1 Creating a Sequential-Access File Using Object Serialization
This section and Section 15.5.2 create and manipulate sequential-access files using object
serialization. The object serialization we show here is performed with byte-based streams,
so the sequential files created and manipulated will be binary files . Recall that binary files
typically cannot be viewed in standard text editors. For this reason, we write a separate ap-
plication that knows how to read and display serialized objects. We begin by creating and
writing serialized objects to a sequential-access file. The example is similar to the one in
Section 15.4, so we focus only on the new features.
Defining Class Account
We begin by defining class Account (Fig. 15.9), which encapsulates the client record in-
formation used by the serialization examples. These examples and class Account are all lo-
cated in the SerializationApps directory with the chapter's examples. This allows class
Account to be used by both examples, because their files are defined in the same default
package. Class Account contains private instance variables account , firstName , last-
Name and balance (lines 7-10) and set and get methods for accessing these instance vari-
ables. Though the set methods do not validate the data in this example, they should do so
in an “industrial-strength” system. Class Account implements interface Serializable
(line 5), which allows objects of this class to be serialized and deserialized with ObjectOut-
putStream s and ObjectInputStream s, respectively. Interface Serializable is a tagging
interface . Such an interface does not contain methods. A class that implements Serializ-
able is tagged as being a Serializable object. This is important, because an ObjectOut-
putStream will not output an object unless it is a Serializable object, which is the case
for any object of a class that implements Serializable .
1
// Fig. 15.9: Account.java
2
// Serializable Account class for storing records as objects.
3
4
5
6
import java.io.Serializable;
public class Account implements Serializable
{
7
private int account;
8
private String firstName;
9
private String lastName;
10
private double balance;
11
Fig. 15.9 | Account class for serializable objects. (Part 1 of 3.)
 
 
Search WWH ::




Custom Search