Home : Course Map : Chapter 9 : Java :
Object I/O
JavaTech
Course Map
Chapter 9

Introduction
Overview
Streams
Wrappers,Buffers
Console I/O
  Text Output 
     Demo 1

  Formatter/printf()
     Demo 2

  Tex 2t Input
     Demo 3

  Scanner
     
Demo 4
File Class
  File I/O
  File Output-Text
     Demo 5

  Formatter to File
     Demo 6

  File Input - Text
    Demo 7

  Scanner - Files
     Demo 8

  File I/O - Binary
     Demo 9
   Demo 10
File Chooser Dialog
  Demo 11

Character Codes
  Demo 12
   Demo13
Object I/O
Types to Bytes
Stream Filters
Other I/O Topics
Exercises

    Supplements
Character I/O
  Demo 1   Demo 2
Random Access
  Demo 3
ZIP/GZIP Streams
  Demo 4
Piped Streams
  Demo 5
NIO Framework
More NIO
  Demo 6

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

So far we have seen that we can do I/O with primitive data types and also with String objects. With the ObjectInputStream and ObjectOutputStream you can, in fact, do I/O with other types of objects as well.

The writeObject (Object) method in ObjectOutputStream grabs the data from the class fields of an existing object and sends that data through the stream. Similarly, the readObject() method in ObjectInputStream can read this data from its stream and fill the data fields in a new instance of the class.

The data is sent sequentially, or serially, a byte at a time on the stream and so this process is also referred to as serialization.

For a class to work with these methods, it must implement the Serializable interface. This interface has no methods but tags the class as suitable for serializing. There are, for example, security concerns with regard to I/O with objects since the internal data of an object could become exposed during the transfer. So not all classes are suitable for serializing.

Many core language classes implement Serializable but you should check the Java 2 API specifications to make sure for a particular class of interest.

Within a class there may be data that is only temporary, that is, the values are only useful for a short time during the running of a program and don't need to be saved. These data fields can be labeled transient and will not be included in the serialization.

When an object to be serialized holds references to other objects, those objects will also be serialized if they implement Serializable. Otherwise, they should be labeled transient.

So a class for serializing could look as follows:

  public class MyClass implements Serializable {
    int fI,fJ;
    double fValue
    transient int fTmpValue;
    String fTitle;
    OtherClass fOtherClass;
    ... constructors & methods ...
  }

Instances of this class could be saved to a file using a method such as this:

  static public void saveMyClass (MyClass my_object, File file) {
    FileOutputStream file_out = new FileOutputStream (file);
    ObjectOutputStream obj_out_stream = new ObjectOutputStream (file_out);
    obj_out_stream.writeObject (my_object);
    obj_out_stream.close ();
  }

Similarly, to read the object back in from a file we could use a method as follows:

  static public MyClass getMyClass (File file) {
    FileInputStream file_in = new FileInputStream (file);
    ObjectInputStream obj_in_stream = new ObjectInputStream (file_in);
    MyClass my_obj = (MyClass)(obj_in_stream.readObject ());
    obj_in_stream.close ();
    return my_obj;
  }

There are a number of other issues regarding serialization that are beyond the scope of this chapter and book. (See references.) See the Chapter 9: Tech section for an example of object I/O with our histogram class objects.

References & Web Resources

Latest update: Nov. 14, 2004

              Tech
Histogram I/O
Hist I/O - Get/Set
  Demo 1
Hist I/O - Objects
  Demo 2
HistogramStream
  Demo 3
Filtering Data
  Demo 4
Exercises

           Physics
Physics Model
Simulation Design
Physics Simulator
  Demo 1
Experiment Design
Experiment Sim.
  Demo 2
Analysis
Expt. + Analysis
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.