Java Reference
In-Depth Information
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class PersonDeserializationTest {
public static void main(String[] args) {
// The input file
File fileObject = new File("person.ser");
try (ObjectInputStream ois =
new ObjectInputStream(new FileInputStream(fileObject))) {
// Read (or deserialize) the three objects
Person john = (Person)ois.readObject();
Person wally = (Person)ois.readObject();
Person katrina = (Person)ois.readObject();
// Let's display the objects that are read
System.out.println(john);
System.out.println(wally);
System.out.println(katrina);
// Print the input path
System.out.println("Objects were read from " +
fileObject.getAbsolutePath());
}
catch(FileNotFoundException e) {
FileUtil.printFileNotFoundMsg(fileObject.getPath());
}
catch(ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
}
Name: John, Gender: Male, Height: 6.7
Name: Wally, Gender: Male, Height: 5.7
Name: Katrina, Gender: Female, Height: 5.4
Objects were read from C:\book\javabook\person.ser
Externalizable Object Serialization
In the previous sections, I showed you how to serialize and deserialize Serializable objects. In this section, I will
show you how to serialize and deserialize Externalizable objects. I have modified the Person class to implement the
Externalizable interface. The new class is called PersonExt and is shown in Listing 7-26.
 
Search WWH ::




Custom Search