Java Reference
In-Depth Information
Listing 7-26.
A PersonExt Class That Implements the Externalizable Interface
// PersonExt.java
package com.jdojo.io;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class PersonExt implements Externalizable {
private String name = "Unknown";
private String gender = "Unknown" ;
private double height = Double.NaN;
// We must define a no-arg constructor for this class. It is
// used to construct the object during deserialization process
// before the readExternal() method of this class is called
public PersonExt() {
}
public PersonExt(String name, String gender, double height) {
this.name = name;
this.gender = gender;
this.height = height;
}
// Override the toString() method to return the person description
public String toString() {
return "Name: " + this.name + ", Gender: " + this.gender +
", Height: " + this.height ;
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// Read name and gender in the same order they were written
this.name = in.readUTF();
this.gender = in.readUTF();
}
public void writeExternal(ObjectOutput out) throws IOException {
// we write only the name and gender to the stream
out.writeUTF(this.name);
out.writeUTF(this.gender);
}
}
Java will pass the reference of the object output stream and object input stream to the
writeExternal()
and
readExternal()
methods of the
PersonExt
class, respectively.
In the
writeExternal()
method, you write the
name
and
gender
fields to the object output stream. Note that the
height
field is not written to the object output stream. It means that you will not get the value of the
height
field back
when you read the object from the stream in the
readExternal()
method. The
writeUTF()
method is used to write
strings (
name
and
gender
) to the object output stream.