Java Reference
In-Depth Information
for objects in addition to primitive-type values and strings. Since ObjectInputStream /
ObjectOutputStream contains all the functions of DataInputStream /
DataOutputStream , you can replace DataInputStream / DataOutputStream completely
with ObjectInputStream / ObjectOutputStream .
ObjectInputStream extends InputStream and implements ObjectInput and
ObjectStreamConstants , as shown in Figure  17.16. ObjectInput is a subinterface of
DataInput ( DataInput is shown in Figure 17.9). ObjectStreamConstants contains the
constants to support ObjectInputStream / ObjectOutputStream .
VideoNote
Object I/O
«interface»
ObjectStreamConstants
java.io.InputStream
«interface»
java.io.DataInput
«interface»
java.io.ObjectInput
java.io.ObjectInputStream
+ObjectInputStream(in: InputStream)
+readObject(): Object
Reads an object.
F IGURE 17.16
ObjectInputStream can read objects, primitive-type values, and strings.
ObjectOutputStream extends OutputStream and implements ObjectOutput and
ObjectStreamConstants , as shown in Figure 17.17. ObjectOutput is a subinterface of
DataOutput ( DataOutput is shown in Figure 17.10).
«interface»
ObjectStreamConstants
java.io.OutputStream
«interface»
java.io.DataOutput
«interface»
java.io.ObjectOutput
java.io.ObjectOutputStream
+ObjectOutputStream(out: OutputStream)
+writeObject(o: Object): void
Writes an object.
F IGURE 17.17
ObjectOutputStream can write objects, primitive-type values, and strings.
You can wrap an ObjectInputStream / ObjectOutputStream on any InputStream /
OutputStream using the following constructors:
// Create an ObjectInputStream
public ObjectInputStream(InputStream in)
// Create an ObjectOutputStream
public ObjectOutputStream(OutputStream out)
Listing 17.5 writes student names, scores, and the current date to a file named object.dat .
L ISTING 17.5
TestObjectOutputStream.java
1 import java.io.*;
2
3 public class TestObjectOutputStream {
 
 
Search WWH ::




Custom Search