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.