Using the StringReader class

The StringReader class is not of much use by itself. What you can do with a StringReader alone is often better done with the methods of the String class.
The StringReader class can however be convenient in use with other classes that require an argument of type Reader.
In this example we show a simple example by using the StreamTokenizer to count words in a String (this can for sure be done other ways too but as said, it’s an example).
We create a StringReader object and pass it to a StreamTokenizer that handles the reading and parsing from it to count how many words there are in the String.

If you run the code the output will be 9 words.

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?