Principle 11.6.3. EFFECTIVE DESIGN: I/O Design.
If an object is going to be input and output to and from files, it should define its own I/O methods. An object contains all the relevant information needed to perform I/O correctly.
java.io
package also provides methods for reading and writing objects, a process known as object serialization. Objects can be converted into a sequence of bytes, or serialized, by using the ObjectOutputStream
class, and they can be deserialized, or converted from bytes into a structured object, by using the ObjectInputStream
class (Figure 11.6.1). Despite the complexity of the serialization/deserialization processes, the methods in these classes make the task just as easy as reading and writing primitive data.Serializable
Interface
Student
class (Listing 11.6.2). In order to serialize an object, it must be a member of a class that implements the Serializable
interface. The Serializable
interface is a marker interface, an interface that doesn’t define any methods or constants but just serves to designate whether an object can be serialized or not.Student
class contains its own I/O methods, readFromFile()
and writeToFile()
. This is an appropriate object-oriented design. The Student
class encapsulates all the relevant information needed to read and write its data.writeToFile()
method, which performs the output task. This method’s FileOutputStream
parameter is used to create an ObjectOutputStream
, whose writeObject()
method writes the object into the file. To output a Student
object, we merely invoke the writeObject()
method. This method writes out the current values of all the object’s public and private fields. In this case, the method would write a String
for the object’s name
, an int
for the object’s year
, and a double
for the object’s gpa
.writeObject()
method can also handle fields that refer to other objects. For example, suppose our Student
object provided a field for courses
that contained a reference to an array of objects, each of which described a course the student has taken. In that case, the writeObject()
method would serialize the array and all its objects (assuming they are serializable). Thus, when a complex object is serialized, the result would be a complex structure that contains all the data linked to that root object.readFromFile()
method, is simply the reverse of the serialization process. The readObject()
method reads one serialized object from the ObjectInputStream
. Its result type is Object
, so it is necessary to cast the result into the proper type. In our example we use a local Student
variable to store the object as it is input. We then copy each field of the local object to this
object.readFromFile()
method throws both the IOException
and ClassNotFoundException
. An IOException
will be generated if the file you are attempting to read does not contain serialized objects of the correct type. Objects that can be input by readObject()
are those that were output by writeObject()
. Thus, just as in the case of binary I/O, it is best to design an object’s input and output routines together so that they are compatible. The ClassNotFoundException
will be thrown if the Student
class cannot be found. This is needed to determine how to deserialize the object.ObjectOutputStream
and ObjectInputStream
, should be used whenever an object needs to be input or output from a stream.ObjectIO
Class
Student
class, let’s now write a user interface that can read and write Student
objects. We can use the same interface we used in the BinaryIO
program. The only things we need to change are the writeRecords()
and readRecords()
methods. Everything else about this program will be exactly the same as in BinaryIO
.ObjectIO
class. Note that the writeRecords()
method will still write five random records to the data file. The difference in this case is that we will call the Student.writeToFile()
method to take care of the actual output operations. The revised algorithm will create a new Student
object, using randomly generated data for its name, year, and GPA and then invoke its writeToFile()
to output its data. Note how a FileOutputStream
is created and passed to the Student.\-writeToFile()
method.readRecords()
method (Fig. Listing 11.6.5, Part II) will read data from a file containing serialized Student
objects. To do so, it first creates a Student object and then invokes its readFromFile()
method, passing it a FileInputStream
. Note how the FileInputStream
is created and, unlike in BinaryIO
, the inner try block is exited by an IOException rather than an EOFException
.SomeObject
s be readable by either the BinaryIO
or the ObjectIO
programs? Explain.