Java Reference
In-Depth Information
ator at the end of the comment line, so we use nextLine to skip over that
to the next line.
Another way to get the scanner to skip comments would be to make the
comment pattern part of the delimiter pattern. But that is not quite as
straightforward as it might sound. We leave that approach as an exer-
cise for the reader.
So skipping comments was trivial with StreamTokenizer and quite in-
volved with Scanner . In contrast, it is quite simple to change the scan-
ner's delimiter to parse a comma-separated-variable file, but to do the
same with StringTokenizer requires careful manipulation of the charac-
ter classes to make the comma a whitespace character and to stop
space from being considered a whitespace character. Although relatively
simple to state, the API makes it awkward to do and it is conceptually
bizarre.
StreamTokenizer is very good for working with free-format files such as
that used in the attribute reading example on page 533 . It read input
that consisted of names and values, seperated by whitespace, with an
optional = character in between, and stored them into an Attr object.
The names were simply words, and values could be words or numbers,
while the = character was an ordinary character. Pairing of names and
values was trivially done, as was detecting a misplaced = character. In
contrast, Scanner has a lot of trouble dealing with such a flexible format.
If you add the = character to the delimiter it isn't too hard to simply treat
every two words as a name and value pair. However, because delimiters
are ignored, you can't detect misplaced = characters. If you don't make
the = character a delimiter, then you have problems when there is no
whitespace between the = and the name or value.
If you wanted to store Attr objects and read them back with a Scanner ,
you could have more flexibility with the values than if you used
StreamTokenizer , but the file would be more restricted in format. For ex-
ample, here is a pair of methods to print and scan attributes:
public static void printAttrs(Writer dest, Attr[] attrs) {
PrintWriter out = new PrintWriter(dest);
 
Search WWH ::




Custom Search