Java Reference
In-Depth Information
6
L ISTING 6.2
Continued
try {
// Create the ObjectInputStream passing it the
// FileInputStream object that points to our
// persistent storage.
ObjectInputStream is = new ObjectInputStream(
new FileInputStream(“file.dat”));
// Read the stored object and downcast it back to
// a StudentList
list = (StudentList)is.readObject();
is.close();
}
catch (IOException e) {
System.err.println(e.getMessage());
}
catch (ClassNotFoundException ce) {
System.err.println(ce.getMessage());
}
return list;
}
public void invoke() {
StudentList list = new StudentList();
buildStudentList(list);
System.out.println(“Before being serialized.”);
list.listStudents();
putStudentList(list);
System.out.println(“After being read back in.”);
// Get the StudentList and print it out
StudentList inList = getStudentList();
if ( inList != null ) {
inList.listStudents();
}
else {
System.err.println(“readObject failed.”);
}
 
Search WWH ::




Custom Search