BufferedInputStream

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.ordinaryChar('.');
  streamTokenizer.wordChars(''', ''');

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

Output:

.
.
.
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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button