Java Reference
In-Depth Information
// Print the output path
System.out.println("Objects were written to " +
fileObject.getAbsolutePath());
}
catch(IOException e1) {
e1.printStackTrace();
}
}
}
Name: John, Gender: Male, Height: 6.7
Name: Wally, Gender: Male, Height: 5.7
Name: Katrina, Gender: Female, Height: 5.4
Objects were written to C:\book\javabook\personext.ser
Listing 7-28. Deserializing PersonExt Objects That Implement the Externalizable Interface
// PersonExtDeserializationTest.java
package com.jdojo.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class PersonExtDeserializationTest {
public static void main(String[] args) {
// The input file
File fileObject = new File("personext.ser");
try (ObjectInputStream ois
= new ObjectInputStream(new FileInputStream(fileObject))) {
// Read (or deserialize) the three objects
PersonExt john = (PersonExt) ois.readObject();
PersonExt wally = (PersonExt) ois.readObject();
PersonExt katrina = (PersonExt) 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) {
Search WWH ::




Custom Search