Home » Core Java » io » BufferedInputStream » StreamTokenizer from BufferedReader example

About Ilias Tsagklis

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

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments