23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11. Object Serialization<br />

<strong>Java</strong> I/O<br />

The last several chapters have shown you how to read and write <strong>Java</strong>'s fundamental data types<br />

(byte, int, String, etc.). However, there's been one glaring omission. <strong>Java</strong> is a fully objectoriented<br />

language; and yet aside from the special case of strings, you haven't seen any<br />

general-purpose methods for reading or writing objects.<br />

Object serialization, first used in the context of Remote Method Invocation (RMI) and later<br />

for <strong>Java</strong>Beans, addresses this need. The java.io.ObjectOutputStream class provides a<br />

writeObject() method you can use to write a <strong>Java</strong> object onto a stream. The<br />

java.io.ObjectInputStream class has a readObject() method you can use to read an<br />

object from a stream. In this chapter you'll learn how to use these two classes to read and<br />

write objects as well as how to customize the format used for serialization.<br />

11.1 Reading and Writing Objects<br />

Object serialization saves an object's state in a sequence of bytes so that the object can be<br />

reconstituted from those bytes at a later time. Serialization in <strong>Java</strong> was first developed for use<br />

in RMI. RMI allows an object in one virtual machine to invoke methods in an object in<br />

another virtual machine, possibly in a different computer on the other side of the planet, by<br />

sending arguments and return values across the Internet. This requires a way to convert those<br />

arguments and return values to and from byte streams. It's a trivial task for primitive data<br />

types, but you need to be able to convert objects as well. That's what object serialization<br />

provides.<br />

Object serialization is also used in the <strong>Java</strong>Beans component software architecture. Bean<br />

classes are loaded into visual builder tools like the BeanBox (shown in Figure 11.1) or<br />

Borland's JBuilder. The designer then customizes the beans by assigning fonts, sizes, text, and<br />

other properties to each bean and connects them together with events. For instance, a button<br />

bean generally has a label property that is encoded as a string of text ("Start" in the button in<br />

Figure 11.1). The designer can change this text.<br />

Figure 11.1. The BeanBox showing a Juggler bean and an ExplicitButton bean<br />

Once the designer has assembled and customized the beans, the form containing all the beans<br />

must be saved. It's not enough to save the bean classes themselves; the customizations that<br />

have been applied to the beans must also be saved. That's where serialization comes in: it<br />

237

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!