Extract Tokens with StreamTokenizer : StreamTokenizer « File Input Output « Java






Extract Tokens with StreamTokenizer

 
import java.io.FileReader;
import java.io.StreamTokenizer;

class ExtractTokens {
  public static void main(String[] args) throws Exception {
    FileReader fr = null;

    fr = new FileReader(args[0]);

    StreamTokenizer st = new StreamTokenizer(fr);

    while (st.nextToken() != StreamTokenizer.TT_EOF) {
      switch (st.ttype) {
      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);
      }
    }
  }
}

   
  








Related examples in the same category

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