Java Reference
In-Depth Information
3.
Although you save the data to a file named saved.txt , the reality is that you are creating a binary
file and not a text file, which explains why the text looks all garbled when you try to open it with
a text editor. You are looking at raw binary data representing primitive values and objects, stored
according to a format defined by the JVM.
4.
Things to try: change ObjectOutputStream to DataOutputStream . Which variables can be writ-
ten? Which cannot? Try expanding this class to use ObjectInputStream to read the saved data
back in again. Take care to read the data in the correct order as saved in the file.
A common use case for using data and object streams is to implement input and output (communi-
cation) between different running programs, for example to send objects or data from one process
to another. This concept is called inter-process communication (IPC). While discussing the details of
IPC is too advanced for a beginner's book on programming, it is worth knowing that this exists and
can in fact be implemented in various ways:
Using a file: One program writes information to a file, which is then read out by another pro-
gram. This method is crude, but works and is relatively simple to implement. After reading
through this chapter, you'll know how to read and write from files, and the Data and Object
streams mentioned above can be utilized here to read from and write to these files.
Sending a signal: This works only if signaling capabilities are provided by the underlying
operating system. Oftentimes it's used to send commands instead of larger data.
A network socket: A data stream sent over a network interface. Note that this allows the com-
municating programs to also reside on different machines. Chapter 10 provides a basic intro-
duction regarding building web services using Java, which can also be used to enable IPC.
A pipe: This is a data stream that allows two-way character-by-character communication,
supported and provided by most operating systems.
A message queue: Also provided by the operating system. Similar to a pipe, but messages are
sent in packets rather than streamed character by character.
Shared memory: Two programs are given access to the same part of memory.
a Word aBout serialization
We have stated that objects can be saved and read using ObjectOutputStream
and ObjectInputStream as long as they implement the Serializable marker
interface. We call this a “marker” interface, as the interface does not actually
specify any methods that must be implemented by the class that is to be serialized.
Any object can be serialized, as long as it implements this marker interface and all
its fields can be serialized, meaning that the fields that should be primitive types are
other objects that can be serialized (implementing the Serializable interface).
Note that it is possible to specifiy fields that should not be serialized using the
transient keyword, for instance:
 
Search WWH ::




Custom Search