Main Page

Previous Next


In this chapter we have explored how we can write objects to a file and read them back. Making your class serializable makes it very easy to save your application data in a file. While what we have discussed is by no means exhaustive, you now know enough to deal with straightforward object serialization. The important points in this chapter are:

  • To make objects of a class serializable the class must implement the Serializable interface.

  • If a class has a superclass that does not implement the Serializable interface, then the superclass must have a public default constructor if it is to be possible to serialize the class.

  • Objects are written to a file using an ObjectOutputStream object and read from a file using and ObjectInputStream object.

  • Objects are written to a file by calling the writeObject() method for the ObjectOutputStream object corresponding to the file.

  • Objects are read from a file by calling the readObject() method for the ObjectInputStream object corresponding to the file.

  • When necessary, for instance if a superclass is not serializable, you can implement the readObject() and writeObject() methods for your classes.

  • A good horse cannot be of a bad color.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor