Java Reference
In-Depth Information
perror ( "write2" );
return
return 1 ;
}
iif ( close ( fd ) < 0 ) {
perror ( "close!?" );
return
return 1 ;
}
return
return 0 ;
}
The same technique can be used in the other direction, of course, and when exchanging data
over a network socket, and anyplace else you need to exchange binary data between Java and
C.
Saving and Restoring Java Objects
Problem
You need to write and (later) read objects.
Solution
Use the object stream classes, ObjectInputStream and ObjectOutputStream .
Discussion
Object serialization is the ability to convert in-memory objects to an external form that can
be sent serially (a byte at a time) and back again. The “and back again” may happen at a later
time, or in another JVM on another computer (even one that has a different byte or-
der)—Java handles differences between machines. ObjectInputStream and ObjectOut-
putStream are specialized stream classes designed to read and write objects. They can be
used to save objects to disk, as I'll show here, and are also useful in passing objects across a
network connection, as I'll show in Reading and Writing Serialized Data .
As you might imagine, if we pass an object (e.g., a MyData object), to the writeObject()
method, and writeObject() notices that one of the fields is itself a reference to an object
Search WWH ::




Custom Search