23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Java</strong> I/O<br />

PushbackReader has two constructors, both of which take an underlying reader as an<br />

argument. The first uses a one-character pushback buffer; the second sets the pushback buffer<br />

to a specified size:<br />

public PushbackReader(Reader in)<br />

public PushbackReader(Reader in, int size)<br />

The PushbackReader class has the usual collection of read() methods. These methods first<br />

try to read the requested characters from the pushback buffer and only read from the<br />

underlying reader if the pushback buffer is empty or has too few characters.<br />

public int read() throws <strong>IO</strong>Exception<br />

public int read(char[] text, int offset, int length) throws <strong>IO</strong>Exception<br />

PushbackReader also has ready(), markSupported(), and close() methods:<br />

public boolean ready() throws <strong>IO</strong>Exception<br />

public boolean markSupported()<br />

public void close() throws <strong>IO</strong>Exception<br />

The ready() and close() methods merely invoke the ready() and close() methods of the<br />

underlying reader. The markSupported() method returns false; pushback readers do not<br />

support marking and resetting.<br />

Three unread() methods push back specific characters. The first pushes back the character c;<br />

the second pushes back the text array; the third pushes back the sub-array of text beginning<br />

at offset and continuing for length chars.<br />

public void unread(int c) throws <strong>IO</strong>Exception<br />

public void unread(char[] text) throws <strong>IO</strong>Exception<br />

public void unread(char[] text, int offset, int length) throws <strong>IO</strong>Exception<br />

The unread characters aren't necessarily the same as the characters that were read. It would be<br />

nicer if the PushbackReader itself kept track of which characters had been read, and all you<br />

had to tell it was how many characters to unread. However, this approach does give the client<br />

programmer the option of inserting tags or other data as the stream is read. The number of<br />

characters you can push back onto the stream is limited by the size of the buffer set in the<br />

constructor. Attempts to unread more characters than can fit in the buffer cause an<br />

<strong>IO</strong>Exception to be thrown. An <strong>IO</strong>Exception is also thrown if you try to unread a closed<br />

reader; once a PushbackReader has been closed, it can be neither read nor unread.<br />

15.12 File Viewer Finis<br />

As a final example of working with readers and writers, we return for the last time to the<br />

FileDumper application last seen in Chapter 13. At that point, we had a GUI program that<br />

allowed any file to be opened and interpreted in one of several formats, including ASCII,<br />

decimal, hexadecimal, short, regular, and long integers in both big- and little-endian formats,<br />

floating point, and double-precision floating point.<br />

In this section we expand the program to read many different text formats besides ASCII. The<br />

user interface must be adjusted to allow a binary choice of whether the file contains text or<br />

386

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!