Java Reference
In-Depth Information
Exercise 12.48 Review the InputReader class of tech-support-complete to check that
you understand how it uses the Scanner class.
Exercise 12.49 Read the API documentation for the Scanner class in the java.util
package. What next methods does it have in addition to those we have discussed in this
section?
Exercise 12.50 Review the Parser class of zuul-better to also see how it uses the
Scanner class. It does so in two slightly different ways.
Exercise 12.51 Review the LoglineTokenizer class of weblog-analyzer to see how it
uses a Scanner to extract the integer values from log lines.
12.9.7
Object serialization
In simple terms, serialization allows a whole object to be written to an external file in a single
write operation and read back in at a later stage using a single read operation. 8 This works
with both simple objects and multi-component objects such as collections. This is a significant
feature that avoids having to read and write objects field by field. It is particularly useful in
applications with persistent data, such as address books or media databases, because it allows
all entries created in one session to be saved and then read back in at a later session. Of course,
we have the choice to write out the contents of objects as text strings, but the process of reading
the text back in again, converting the data to the correct types, and restoring the exact state of a
complex set of objects is often difficult, if not impossible. Object serialization is a much more
reliable process.
Concept:
Serialization
allows whole ob-
jects, and object
hierarchies, to be
read and written
in a single opera-
tion. Every object
involved must be
from a class that
implements the
Serializable
interface.
In order to be eligible to participate in serialization, a class must implement the
Serializable interface that is defined in the java.io package. However, it is worth not-
ing that this interface defines no methods. This means that the serialization process is man-
aged automatically by the runtime system and requires little user-defined code to be written.
In the address-book-io project, both AddressBook and ContactDetails implement this
interface so that they can be saved to a file. The AddressBookFileHandler class defines
the methods saveToFile and readFromFile to illustrate the serialization process. Code
12.24 contains the source of saveToFile to illustrate how little code is actually required
to save the whole address book, in a single write statement. Note too that, because we are
writing objects in binary form, a Stream object has been used rather than a Writer . The
AddressBookFileHandler class also includes further examples of the basic reading and
writing techniques used with text files. See, for instance, its saveSearchResults and
showSearchResults methods.
8 This is a simplification, because objects can also be written and read across a network, for instance, and
not just within a file system.
 
Search WWH ::




Custom Search