
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - StreamTokenizer class
Introduction
The Java StreamTokenizer class takes an input stream and parses it into "tokens", allowing the tokens to be read one at a time. The stream tokenizer can recognize identifiers, numbers, quoted strings, and various comment styles.
Class declaration
Following is the declaration for Java.io.RandomAccessFile class −
public class StreamTokenizer extends Object
Field
Following are the fields for Java.io.StreamTokenizer class â
double nval â If the current token is a number, this field contains the value of that number.
String sval â If the current token is a word token, this field contains a string giving the characters of the word token.
static int TT_EOF â A constant indicating that the end of the stream has been read.
static int TT_EOL â A constant indicating that the end of the line has been read.
static int TT_NUMBER â A constant indicating that a number token has been read.
static int TT_WORD â A constant indicating that a word token has been read.
int ttype â After a call to the nextToken method, this field contains the type of the token just read.
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 |
StreamTokenizer(Reader r) This creates a tokenizer that parses the given character stream. |
Class methods
Sr.No. | Method & Description |
---|---|
1 |
void commentChar(int ch)
Specified that the character argument starts a single-line comment. |
2 |
void eolIsSignificant(boolean flag)
This method determines whether or not ends of line are treated as tokens. |
3 |
int lineno()
This method returns the current line number. |
4 |
void lowerCaseMode(boolean fl)
This method determines whether or not word token are automatically lowercased. |
5 |
int nextToken()
This method parses the next token from the input stream of this tokenizer. |
6 |
void ordinaryChar(int ch)
This method specifies that the character argument is "ordinary" in this tokenizer. |
7 |
void ordinaryChars(int low, int hi)
This method specifies that all characters c in the range low <= c <= high are "ordinary" in this tokenizer. |
8 |
void parseNumbers()
This method specifies that numbers should be parsed by this tokenizer. |
9 |
void pushBack()
This method causes the next call to the nextToken method of this tokenizer to return the current value in the ttype field, and not to modify the value in the nval or sval field. |
10 |
void quoteChar(int ch)
This method specifies that matching pairs of this character delimit string constants in this tokenizer. |
11 |
void resetSyntax()
This method resets this tokenizer's syntax table so that all characters are "ordinary." See the ordinaryChar method for more information on a character being ordinary. |
12 |
void slashSlashComments(boolean flag)
This method determines whether or not the tokenizer recognizes C++ style comments. |
13 |
void slashStarComments(boolean flag)
This method determines whether or not the tokenizer recognizes C style comments. |
14 |
String toString()
This method returns the string representation of the current stream token and the line number it occurs on. |
15 |
void whitespaceChars(int low, int hi)
This method specifies that all characters c in the range low <= c <= high are white space characters. |
16 |
void wordChars(int low, int hi)
This method specifies that all characters c in the range low <= c >= high are word constituents. |
Methods inherited
This class inherits methods from the following classes â
- Java.io.Object
Example - Use # as a comment character
The following example shows the usage of StreamTokenizer commentChar(int ch) method.
StreamTokenizerDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.Reader; import java.io.StreamTokenizer; import java.io.StringReader; public class StreamTokenizerDemo { public static void main(String[] args) throws IOException { String input = "Java 101 # This is a comment\nPython 202"; Reader reader = new StringReader(input); StreamTokenizer tokenizer = new StreamTokenizer(reader); tokenizer.commentChar('#'); // Treat '#' as start of comment System.out.println("Tokens:"); while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) { if (tokenizer.ttype == StreamTokenizer.TT_WORD) { System.out.println("Word: " + tokenizer.sval); } else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) { System.out.println("Number: " + tokenizer.nval); } } } }
Output
Let us compile and run the above program, this will produce the following result−
Tokens: Word: Java Number: 101.0 Word: Python Number: 202.0
Explanation
Input includes # This is a comment, which gets skipped.
Only Java 101 and Python 202 are parsed as tokens.
Example - eolIsSignificant(true) â Detect end of lines
The following example shows the usage of StreamTokenizer eolIsSignificant(boolean flag) method.
StreamTokenizerDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.Reader; import java.io.StreamTokenizer; import java.io.StringReader; public class StreamTokenizerDemo { public static void main(String[] args) throws IOException { String input = "Hello 123\nWorld 456\nDone"; Reader reader = new StringReader(input); StreamTokenizer tokenizer = new StreamTokenizer(reader); tokenizer.eolIsSignificant(true); // Enable EOL tokens System.out.println("Tokens:"); while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) { switch (tokenizer.ttype) { case StreamTokenizer.TT_WORD: System.out.println("Word: " + tokenizer.sval); break; case StreamTokenizer.TT_NUMBER: System.out.println("Number: " + tokenizer.nval); break; case StreamTokenizer.TT_EOL: System.out.println("End of line"); break; } } } }
Output
Let us compile and run the above program, this will produce the following result−
Tokens: Word: Hello Number: 123.0 End of line Word: World Number: 456.0 End of line Word: Done
Explanation
With eolIsSignificant(true), the tokenizer reports each line break.
You'll see "End of line" printed after each line of tokens.
Example - Print line number for each token
The following example shows the usage of StreamTokenizer lineno() method.
StreamTokenizerDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.Reader; import java.io.StreamTokenizer; import java.io.StringReader; public class StreamTokenizerDemo { public static void main(String[] args) throws IOException { String input = "Java 101\nPython 202\nC 303"; Reader reader = new StringReader(input); StreamTokenizer tokenizer = new StreamTokenizer(reader); tokenizer.eolIsSignificant(true); // Count lines accurately System.out.println("Tokens with line numbers:"); while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) { if (tokenizer.ttype == StreamTokenizer.TT_WORD) { System.out.println("Line " + tokenizer.lineno() + ": Word - " + tokenizer.sval); } else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) { System.out.println("Line " + tokenizer.lineno() + ": Number - " + tokenizer.nval); } else if (tokenizer.ttype == StreamTokenizer.TT_EOL) { // Line numbers increase after TT_EOL } } } }
Output
Let us compile and run the above program, this will produce the following result−
Tokens with line numbers: Line 1: Word - Java Line 1: Number - 101.0 Line 2: Word - Python Line 2: Number - 202.0 Line 3: Word - C Line 3: Number - 303.0
Explanation
Each token is printed along with the line number.
lineno() reflects the line the token was found on.