An ObjectInputStream deserializes objects from an input stream. It is a high - level stream
that needs to be chained to a low - level stream that represents the source of the serialized
objects. You should know how to use FileInputStream with ObjectInputStream , and I
discuss an example of using these two classes as we deserialize the two Contact2 objects
from the SerializeDemo program in the previous sectionThe ObjectInputStream class contains read methods that correspond to the write methods
of ObjectOutputStream , including public Object readObject() throws IOException,
ClassNotFoundException .
The readObject method deserializes the next object in the stream. Note the reference
type of the return value is Object . Typically we need to cast the reference to its appropriate
class type.
The following DeserializeDemo program deserializes two objects from the mycontacts.
ser fi le created in SerializeDemo .
that needs to be chained to a low - level stream that represents the source of the serialized
objects. You should know how to use FileInputStream with ObjectInputStream , and I
discuss an example of using these two classes as we deserialize the two Contact2 objects
from the SerializeDemo program in the previous sectionThe ObjectInputStream class contains read methods that correspond to the write methods
of ObjectOutputStream , including public Object readObject() throws IOException,
ClassNotFoundException .
The readObject method deserializes the next object in the stream. Note the reference
type of the return value is Object . Typically we need to cast the reference to its appropriate
class type.
The following DeserializeDemo program deserializes two objects from the mycontacts.
ser fi le created in SerializeDemo .
1. package com.sybex.io;
2.
3. import java.io.*;
4. import java.util.GregorianCalendar;
5.
6. public class DeserializeDemo {
7. public static void main(String [] args) {
8. try {
9. File contactsFile = new File(“mycontacts.ser”);
10. FileInputStream fis =
11. new FileInputStream(contactsFile);
12. ObjectInputStream in = new ObjectInputStream(fis);
13.
14. while(fis.available() > 0) {
15. Object obj = in.readObject();
16. if(obj instanceof Contact2) {
17. Contact2 contact = (Contact2) obj;
18. System.out.println(contact);
19. System.out.println(“city = “
20. + contact.getCity());
21. }
22. }
23.
24. in.close();
25. fis.close();
26. }catch(IOException e) {
27. e.printStackTrace();
28. }catch(ClassNotFoundException c) {
29. c.printStackTrace();
30. }
31. }
32. }
2.
3. import java.io.*;
4. import java.util.GregorianCalendar;
5.
6. public class DeserializeDemo {
7. public static void main(String [] args) {
8. try {
9. File contactsFile = new File(“mycontacts.ser”);
10. FileInputStream fis =
11. new FileInputStream(contactsFile);
12. ObjectInputStream in = new ObjectInputStream(fis);
13.
14. while(fis.available() > 0) {
15. Object obj = in.readObject();
16. if(obj instanceof Contact2) {
17. Contact2 contact = (Contact2) obj;
18. System.out.println(contact);
19. System.out.println(“city = “
20. + contact.getCity());
21. }
22. }
23.
24. in.close();
25. fis.close();
26. }catch(IOException e) {
27. e.printStackTrace();
28. }catch(ClassNotFoundException c) {
29. c.printStackTrace();
30. }
31. }
32. }
The while loop on line 14 reads in objects from the mycontacts.ser fi le until the stream
is empty. Two objects are in the fi le, so the while loop executes twice. The instanceof
comparison on line 16 is true for both objects and line 18 prints the Contact2 object,
outputting the result of its toString method. I added line 19 to explicitly illustrate that
the city fi eld did not get serialized because it was declared transient . Transient fi elds are
ignored during serialization and initialized to their “ zero ” value during deserialization,
which for references is null . The output of the DeserializeDemo program is Bugs Bunny 22 2025551212
city = null
Daffy Duck 33 3035551212
city = null
By the way, the GregorianCalendar objects were also serialized, but the toString
method for GregorianCalendar prints out a lot of information, so I purposely left it out of
the toString method of Contact2 . (We discuss formatting dates in detail later in this chapter.)
If you open mycontacts.ser in a text editor, you will clearly see the two Gregorian-
Calendar objects serialized in the fi le.
is empty. Two objects are in the fi le, so the while loop executes twice. The instanceof
comparison on line 16 is true for both objects and line 18 prints the Contact2 object,
outputting the result of its toString method. I added line 19 to explicitly illustrate that
the city fi eld did not get serialized because it was declared transient . Transient fi elds are
ignored during serialization and initialized to their “ zero ” value during deserialization,
which for references is null . The output of the DeserializeDemo program is Bugs Bunny 22 2025551212
city = null
Daffy Duck 33 3035551212
city = null
By the way, the GregorianCalendar objects were also serialized, but the toString
method for GregorianCalendar prints out a lot of information, so I purposely left it out of
the toString method of Contact2 . (We discuss formatting dates in detail later in this chapter.)
If you open mycontacts.ser in a text editor, you will clearly see the two Gregorian-
Calendar objects serialized in the fi le.
No comments:
Post a Comment