Stream Token : StreamTokenizer « File Input Output « Java






Stream Token

  
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class StreamTokenApp {
  public static void main(String args[]) throws IOException {
    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer inStream = new StreamTokenizer(inData);
    inStream.commentChar('#');
    boolean eof = false;
    do {
      int token = inStream.nextToken();
      switch (token) {
      case inStream.TT_EOF:
        System.out.println("EOF encountered.");
        eof = true;
        break;
      case inStream.TT_EOL:
        System.out.println("EOL encountered.");
        break;
      case inStream.TT_WORD:
        System.out.println("Word: " + inStream.sval);
        break;
      case inStream.TT_NUMBER:
        System.out.println("Number: " + inStream.nval);
        break;
      default:
        System.out.println((char) token + " encountered.");
        if (token == '!')
          eof = true;
      }
    } while (!eof);
  }
}

   
  








Related examples in the same category

1.Create StreamTokenizer from BufferedReader
2.Create StreamTokenizer from FileReader
3.Extract Tokens with StreamTokenizer
4.Read file with StreamTokenizer
5.Set whitespaceChars
6.Set eolIsSignificant
7.Deal with StreamTokenizer.TT_WORD, StreamTokenizer.TT_NUMBER
8.Using StreamTokenizer to count words and numbers in a file
9.Tokenizing Java Source Code