Java Reference
In-Depth Information
|| f.isDirectory ();
}
public String getDescription () {
return "Java files (*.java)";
}
}
We b
Course
Chapter
9
discusses
a
similar
approach
for
setting
up
a
JFileChooser to save a file.
9.13 Histogram I/O
Here we use our histogram classes to illustrate some of the I/O topics discussed
in this chapter. We add the capability to save the histograms to disk files and to
read them back from the files. We also create a histogram stream class in which
a histogram becomes the destination of a stream of data. Similarly, we create a
stream filter than calibrates the data heading for a histogram.
9.13.1 Saving histograms
An obviously useful feature for our histogram classes would be the ability to save
a histogram to a disk file and to read it back later. The data in the class Histogram
includes the number of bins, the bin array, the upper and lower range values, the
underflow and overflow values, plus the title and axis label. (Note that this data
involves double , int , and String types.)
One approach to saving the histogram is to obtain the data from the histogram
and use the file output techniques discussed above to write the various data values
to a file. The data could be made directly accessible or provided with a set of
“get” (or “getter”) methods, such as getTitle() to obtain the title string and
getBins() to return the integer array holding the bin contents. The histogram
could be rebuilt by reading in the data with the file input techniques, creating a
new histogram object, and then filling its values with “set” (or “setter”) methods,
such as setTitle (String title) .
The class HistIOTools (see the listing in the Web Course Chapter 9: Te ch
section) provides a group of static methods to do this except that it first writes
the data into a byte array using the techniques discussed in Section 9.10 and
then writes this array to a file. Conversely, it provides methods to read this array
back from a file, extract the data, and load the data into a new histogram object.
Packaging the data into a single-byte array reduces the I/O operations with the file.
The most elegant approach, however, for adding I/O to our histograms is to
use the object serialization techniques and simply stream a Histogram object to
a file. For example, if Histogram were made Serializable ,wecould create
 
Search WWH ::




Custom Search