Home : Course Map : Chapter 9 : Java : Tech :
Input/Output for Histograms
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

A useful feature for our histogram classes would be the capability to save histogram data (including bin values, range, titles) to a disk file and conversely to read histogram data from a file and recreate the histogram from this data.

To rebuild an instance of the base class Histogram we would need the bin array, the upper and lower range, the under and over flow values, and also the text information such as the title and axes labels. For the subclasses we would need additional data values as well.

To provide histogram I/O there are several approaches to the class design:

  • Getters/Setters
    Provide get[Field]() and set[Field]() methods in the Histogram class for all of the data fields necessary to save and rebuild the histograms. An application method can then "get" the data from a histogram and write the data to a file. Conversely, an application could read in the data from the file and create a new Histogram object and fill its data fields with the values from the saved histogram using the setter methods or via the arguments of a constructor.

    Demo 1

  • Stream Wrappers
    A variation on the above approach is to create a new class (or classes) whose only job is to write or read a histogram to/from a file. Such a wrapper could use gettter/setter methods if available or, if it has access privileges, directly access the histogram class fields. For example, if the fields are protected, a wrapper in the same package can directly access the fields.

  • Serialized Objects
    The most elegant approach is simply to make the histogram classes Serializable and save the histogram objects to a disk file by writing them to an output object stream. To rebuild the histograms, just open an input object stream to the file and read them in. We discussed in Chapter 9: Java : Object I/O the general techniques for creating and using object streams.

    Demo 2

We demonstrate both the getter/setter and serialized object approaches. The former approach illustrates some useful class design concepts and it also has the advantage that we do not need to modify the Histogram class or its subclasses in any way.

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.