File
Set eolIsSignificant example
In this example we shall show you how to use the eolIsSignificant(boolean flag)
method of a StreamTokenizer to determine whether or not ends of line are treated as tokens. To use eolIsSignificant(boolean flag)
method one should perform the following steps:
- Create a new FileReader.
- Create a new BufferedReader using the fileReader.
- Create a new StreamTokenizer that parses the given bufferedReader.
- Use
eolIsSignificant(boolean flag)
API method of StreamTokenizer that determines whether or not ends of line are treated as tokens. - Iterate over the tokens of the tokenizer. For every token, check the type of the token, using
ttype
method of StreamTokenizer. If the type is equal toTT_EOL
(end of line) a counter is increased,
as described in the code snippet below.
package com.javacodegeeks.snippets.core; import java.io.BufferedReader; import java.io.FileReader; import java.io.StreamTokenizer; class Main { public static void main(String args[]) throws Exception { FileReader fileReader = new FileReader("C:/Users/nikos7/Desktop/output.txt"); BufferedReader buffReader = new BufferedReader(fileReader); StreamTokenizer tokenizer = new StreamTokenizer(buffReader); tokenizer.eolIsSignificant(true); int cnt = 1; while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) { switch (tokenizer.ttype) { case StreamTokenizer.TT_EOL: ++cnt; } } System.out.println("The file has " + cnt + " lines"); fileReader.close(); } }
Output:
The file has 432 lines
This was an example of how to use the eolIsSignificant(boolean flag)
method in a tokenizer in Java.