O'Reilly logo

Java I/O by Elliotte Rusty Harold

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

File Viewer Finis

As a final example of working with readers and writers, we return for the last time to the FileDumper application last seen in Chapter 13. At that point, we had a GUI program that allowed any file to be opened and interpreted in one of several formats, including ASCII, decimal, hexadecimal, short, regular, and long integers in both big- and little-endian formats, floating point, and double-precision floating point.

In this section we expand the program to read many different text formats besides ASCII. The user interface must be adjusted to allow a binary choice of whether the file contains text or numeric data. If they choose text, you’ll need to use a reader to read the file instead of an input stream. You’ll also need to provide some means for the user to pick the encoding they want text read in (e.g., MacRoman, ISO Latin-1, Unicode, etc). Since there are several dozen text encodings, the best choice is a list box. All of this can be integrated into the mode panel. Figure 15.1 shows the revised ModePanel2 class. The code is given in Example 15.9. Two new public methods are added, isText() and getEncoding(). The rest of the changes are fairly minor ones to set up the GUI.

A mode panel with a list box for encodings

Figure 15-1. A mode panel with a list box for encodings

Example 15-9. ModePanel2

import java.awt.*; import javax.swing.*; public class ModePanel2 extends JPanel { JCheckBox bigEndian = new JCheckBox("Big ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required