// // example program using FileReader, FileWriter, // StreamTokenizer, PrintWriter // import java.io.* ; public class TestStreamTokenizer { // command line arguments are infile, outfile // reads from infile and uses StreamTokenizer to do some // parsing. infile can be any text file, but // StreamTokenizer is most useful for text that looks // like Java source. // writes results to outfile using PrintWriter // (the following variables are static for convenience -- // to permit "global" use in main and other static // methods) static StreamTokenizer st ; static PrintWriter pr ; static int lineNum = -1 ; // current line number public static void main(String[] args) { if (args.length < 2) { System.out.println("Two arguments required") ; System.exit(-1) ; } // build StreamTokenizer object reading from // an input stream whose source is infile try { st = new StreamTokenizer( new FileReader(args[0])) ; } catch (FileNotFoundException e) { System.out.println("Input file " + args[0] + " not found") ; System.exit(-1) ; } // override some StreamTokenizer defaults: // skip C-style comments st.slashStarComments(true) ; // skip C++-style comments st.slashSlashComments(true) ; // don't treat '/' alone as a comment st.ordinaryChar('/') ; // don't permit '.' as part of a word st.ordinaryChar('.') ; // build PrintWriter object writing to an output // stream whose destination is outfile try { pr = new PrintWriter( new FileWriter(args[1])) ; } catch (IOException e) { System.out.println("Output file " + args[1] + " could not be created") ; System.exit(-1) ; } // tokenize input and write results to output. try { int tokenType = st.nextToken() ; while (tokenType != StreamTokenizer.TT_EOF) { processToken(tokenType) ; tokenType = st.nextToken() ; } } catch (IOException e) { System.out.println("I/O error during parsing") ; } // closing pr closes chained FileWriter too // observe that most PrintWriter methods don't throw // exceptions, so no need to catch one here pr.close() ; } // pull out code to print info about one token to make main() // less unmanageable static void processToken(int tokenType) { // check for new line number if (lineNum != st.lineno()) { lineNum = st.lineno() ; pr.println() ; pr.println("Line " + lineNum) ; } // print something appropriate based on token type switch (tokenType) { case StreamTokenizer.TT_WORD: pr.print("Word token found") ; pr.print("; string value is " + st.sval) ; break ; case StreamTokenizer.TT_NUMBER: pr.print("Number token found") ; pr.print("; numeric value is " + st.nval) ; break ; case StreamTokenizer.TT_EOL: pr.print("End-of-line token found") ; break ; default: pr.print("Token of type " + tokenType + " found; character value is " + (char) tokenType) ; // for quoted strings, quote character is // returned as token type and string // is returned in sval if (st.sval != null) pr.print("; string value is " + st.sval) ; break ; } pr.println() ; } }