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 ...

Get Java I/O now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.