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 to TT_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.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
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
Back to top button