Java Reference
In-Depth Information
If the record is rewritten beginning at the same location in the file using the new name,
the record will be
300 Pam Worthington 0.00
The new record is larger (has more characters) than the original record. “ Worthington
would overwrite the “ 0.00 ” in the current record and the characters beyond the second
o ” in Worthington will overwrite the beginning of the next sequential record in the
file. The problem here is that fields in a text file—and hence records—can vary in size. For
example, 7, 14, -117, 2074 and 27383 are all int s stored in the same number of bytes (4)
internally, but they're different-sized fields when written to a file as text. Therefore, re-
cords in a sequential-access file are not usually updated in place—instead, the entire file is
rewritten. To make the preceding name change, the records before 300 Pam White 0.00
would be copied to a new file, the new record (which can be of a different size than the
one it replaces) would be written and the records after 300 Pam White 0.00 would be cop-
ied to the new file. Rewriting the entire file is uneconomical to update just one record, but
reasonable if a substantial number of records need to be updated.
15.5 Object Serialization
In Section 15.4, we demonstrated how to write the individual fields of a record into a file
as text, and how to read those fields from a file. When the data was output to disk, certain
information was lost, such as the type of each value. For instance, if the value "3" is read
from a file, there's no way to tell whether it came from an int , a String or a double . We
have only data, not type information, on a disk.
Sometimes we want to read an object from or write an object to a file or over a net-
work connection. Java provides object serialization for this purposes. A serialized object
is an object represented as a sequence of bytes that includes the object's data as well as
information about the object's type and the types of data stored in the object. After a seri-
alized object has been written into a file, it can be read from the file and deserialized —that
is, the type information and bytes that represent the object and its data can be used to rec-
reate the object in memory.
Classes ObjectInputStream and ObjectOutputStream
Classes ObjectInputStream and ObjectOutputStream (package java.io ), which respec-
tively implement the ObjectInput and ObjectOutput interfaces, enable entire objects to
be read from or written to a stream (possibly a file). To use serialization with files, we ini-
tialize ObjectInputStream and ObjectOutputStream objects with stream objects that read
from and write to files. Initializing stream objects with other stream objects in this manner
is sometimes called wrapping —the new stream object being created wraps the stream ob-
ject specified as a constructor argument.
Classes ObjectInputStream and ObjectOutputStream simply read and write the
byte-based representation of objects—they don't know where to read the bytes from or
write them to. The stream object that you pass to the ObjectInputStream constructor
supplies the bytes that the ObjectInputStream converts into objects. Similarly, the stream
object that you pass to the ObjectOutputStream constructor takes the byte-based repre-
sentation of the object that the ObjectOutputStream produces and writes the bytes to the
specified destination (e.g., a file, a network connection, etc.).
 
 
Search WWH ::




Custom Search