THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
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 voidunread(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:


0: 3
1: 4
2: 1
3: 2


Data from the last unread (the one with '3' and '4') is read back first, and within that unread the data
comes from the beginning of the array through to the end. When that data is exhausted, the data from the first
unread is returned in the same order. The unread method copies data into the pushback buffer, so changes
made to an array after it is used with unread do not affect future calls to read.


20.5.12. StreamTokenizer


Tokenizing input text is a common application, and the java.io package provides a StreamTokenizer
class for simple tokenization. A more general facility for scanning and converting input text is provided by the
java.util.Scanner classsee "Scanner" on page 641.


You can tokenize a stream by creating a StreamTokenizer with a Reader object as its source and then
setting parameters for the scan. A scanner loop invokes nextToken, which returns the token type of the next
token in the stream. Some token types have associated values that are found in fields in the
StreamTokenizer object.


This class is designed primarily to parse programming language-style input; it is not a general tokenizer.
However, many configuration files look similar enough to programming languages that they can be parsed by
this tokenizer. When designing a new configuration file or other data, you can save work if you make it look
enough like a language to be parsed with StreamTokenizer.


When nextToken recognizes a token, it returns the token type as its value and also sets the ttype field to
the same value. There are four token types:

Free download pdf