How do I work with the StreamTokenizer to get number and word tokens from a file?

John Zukowski

After opening up a FileReader, you get the next token and switch based upon which type of token you have:

FileReader reader = new FileReader(filename);
BufferedReader br = new BufferedReader(reader);
StreamTokenizer tokenizer = new StreamTokenizer(br);
int type;
while ((type = tokenizer.nextToken()) != StreamTokenizer.TT_EOF) {
  switch (type) {
    case StreamTokenizer.TT_NUMBER:
      System.out.println("Number: " + tokenizer.nval);
    case StreamTokenizer.TT_WORD:
      System.out.println("Word: " + tokenizer.sval);
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact