FileInputStream
Tokenizer from FileReader example
In this example we shall show you how to get a tokenizer from a FileReader. The FileReader is a convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To get a tokenizer from a FileReader one should perform the following steps:
- Create a new FileReader, given the name of the file to read from.
- Create a new StreamTokenizer that parses the given file reader.
- Get the next token of the tokenizer, and check if it a String, the end of a line, a number, a word or something else,
as described in the code snippet below.
package com.javacodegeeks.snippets.core;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class StreamToken {
public static void main(String[] args) throws Exception {
FileReader fr = null;
fr = new FileReader("C:/Users/nikos7/Desktop/output.txt");
StreamTokenizer st = new StreamTokenizer(fr);
st.lowerCaseMode(true);
while (st.nextToken() != StreamTokenizer.TT_EOF) {
switch (st.ttype) {
case ''':
case '"':
System.out.println("String = " + st.sval);
break;
case StreamTokenizer.TT_EOL:
System.out.println("End-of-line");
break;
case StreamTokenizer.TT_NUMBER:
System.out.println("Number = " + st.nval);
break;
case StreamTokenizer.TT_WORD:
System.out.println("Word = " + st.sval);
break;
default:
System.out.println("Other = " + (char) st.ttype);
}
}
}
}
Output:
.
.
.
Other = &
Word = copy
Other = ;
Number = 2012.0
Other = ,
Word = smartjava.org
.
.
.
This was an example of how to get a tokenizer from a FileReader in Java.
