StreamTokenizer from BufferedReader example

This is an example of how to get and use a StreamTokenizer from a BufferedReader. The StreamTokenizer class takes an input stream and parses it into “tokens”, allowing the tokens to be read one at a time. Getting a StreamTokenizer from a BufferedReader implies that you should:

  • Create a new BufferedReader using a FileReader.
  • Create a new StreamTokenizer that parses the given bufferedReader.
  • Iterate over the tokens of the tokenizer and print their values.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;

class StreamToeknizerExample {

    public static void main(String args[]) throws Exception {
  FileReader fileReader = new FileReader(args[0]);

 BufferedReader buffReader = new BufferedReader(fileReader);
  StreamTokenizer streamTokenizer = new StreamTokenizer(buffReader);
  streamTokenizer.wordChars(''', ''');

  while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
switch (streamTokenizer.ttype) {
    case StreamTokenizer.TT_WORD:
  System.out.println(streamTokenizer.lineno() + ") " + streamTokenizer.sval);
    case StreamTokenizer.TT_NUMBER:
  System.out.println(streamTokenizer.lineno() + ") " + streamTokenizer.nval);
  System.out.println(streamTokenizer.lineno() + ") " + (char) streamTokenizer.ttype);


380) I'm
380) an
380) enterprise
380) architect
380) ,
380) working
380) for
380) JPoint
380) .
380) My
380) focus
380) areas
380) are
380) SOA
380) ,
380) governance

This was an example of how to get and use a StreamTokenizer from a BufferedReader in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button