import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.StreamTokenizer; /** * A simple class that prints out all the tokens in the file * specified on the command line. Intended as a mechanism for * experimenting with java.io.StreamTokenizer. Documentation on * that class is available at. * http://java.sun.com/j2se/1.4/docs/api/java/io/StreamTokenizer.html * * @author Samuel A. Rebelsky * @version 1.0 of 23 September 2002 */ public class PrintTokens { public static void main(String[] args) { // Make sure that the program is called correctly. if (args.length != 1) { System.err.println("Usage: java Test filename"); System.exit(1); } // if (args.length != 1) try { // Local variables int tok; // The number of the token // Create the token stream. StreamTokenizer tokenStream = new StreamTokenizer(new FileReader(args[0])); // Update any characteristics of the stream. // (Nothing right now) // Repeatedly read tokens until we're out of tokens. while ((tok = tokenStream.nextToken()) != StreamTokenizer.TT_EOF) { // Print the token id. System.out.print("("); System.out.print(tok); System.out.print("): "); // Print the token value. System.out.println(tokenStream.toString()); } } // try // Whoops! Tried to tokenize a file that doesn't exist. catch (FileNotFoundException fnfe) { System.err.println("Error: " + fnfe.toString()); } // catch // Whoops! Something went seriously wrong in nextToken(). catch (IOException ioe) { System.err.println("I/O Error: " + ioe.toString()); } } // main(String[]) } // class PrintTokens