Java Reference
In-Depth Information
This method follows the exact three-step process discussed earlier. It creates the appropriate
streams for writing and writes the object to persistent storage.
6
The second section to study is the getStudentList() method. It does the exact opposite of the
putStudentList() method. One important step in this method that is not part of its counter-
part is the downcast from an object to a StudentList . You must do this because when the
StudentList object was stored, it was upcast to an Object . The getStudentList() method is
listed as follows:
public StudentList getStudentList() {
StudentList list = null;
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;
}
Build and run the application. The output should look like the following:
Before being serialized.
Student 0 : Bob Robinson
Student 1 : Steve Bobinson
Student 2 : Rob Stevinson
Student 3 : Todd Thompson
Student 4 : Tom Toddson
Student 5 : Rob Bobinson
After being read back in.
 
Search WWH ::




Custom Search