Java Reference
In-Depth Information
Each
Pushback
stream has three variants of
unread
, matching the variants
of
read
. We illustrate the character version of
PushbackReader
, but the
byte equivalents for
PushbackInputStream
have the same behavior:
public void
unread(int c)
tHRows IOException
Pushes back the single character
c
. If there is insufficient
room in the pushback buffer an
IOException
is thrown.
public void
unread(char[] buf, int offset, int count)
throws IOEx-
ception
Pushes back the characters in the specified subarray. The
first character pushed back is
buf[offset]
and the last is
buf[offset+count-1]
. The subarray is prepended to the front of
the pushback buffer, such that the next character to be read
will be that at
buf[offset]
, then
buf[offset+1]
, and so on. If
the pushback buffer is full an
IOException
is thrown.
public void
unread(char[] buf)
tHRows IOException
Equivalent to
unread(buf,0, buf.length)
.
For example, after two consecutive
unread
calls on a
PushbackReader
with
the characters
'1'
and
'2'
, the next two characters read will be
'2'
and
'1'
because
'2'
was pushed back second. Each
unread
call sets its own
list of characters by prepending to the buffer, so the code
pbr.unread(new char[] {'1', '2'});
pbr.unread(new char[] {'3', '4'});
for (int i = 0; i < 4; i++)
System.out.println(i + ": " + (char)pbr.read());
produces the following lines of output: