/** * Parsing character input using StreamTokenizer class. Reads from a text file * and writes back to another text file and to the standard output stream. * Though StreamTokenizer can only extract numbers in decimal notation, * it has enough capability to parse most ASCII files. * By default, StreamTokenizer will parse numbers in decimal notation and skip * the standard white space characters: blanks, end-of-lines, and tabs. * Try the following on your favorite ASCII file and/or standard input * (System.in). * @author Dung "Zung" Nguyen * @since 12/02/2001 */ import java.io.*; public class TestStreamTokenizer { /** * */ public void testInOut(String inFile, String outFile) { try { // Create an input text file object: Reader fileReader = new FileReader(inFile); // Create a Streamtokenizer to parse the input text file: StreamTokenizer tokenizer = new StreamTokenizer(fileReader); // To read from the standard input, comment out the above statement // and un-comment the statement below. // StreamTokenizer tokenizer = new StreamTokenizer(new InputStreamReader(System.in)); // Experiment with the following method calls by un-commenting them // one at a time. //tokenizer.resetSyntax(); //tokenizer.wordChars('0','9'); //tokenizer.wordChars('a','z'); //tokenizer.wordChars('A','Z'); //tokenizer.whitespaceChars(0,' '); //tokenizer.ordinaryChar('.'); //tokenizer.ordinaryChar('('); //tokenizer.ordinaryChar(')'); //tokenizer.ordinaryChar('+'); //tokenizer.ordinaryChar('*'); // Create an output text file object: Writer fileWriter = new FileWriter(outFile); // Create a PrintWriter object to print characters to the FileWriter object: PrintWriter fileOut = new PrintWriter(fileWriter); // fileOut decorates fileWriter! tokenizer.eolIsSignificant(true); // Do NOT ignore the end-of-line character. // Read from fileReader and echo to fileOut and to System.out: while (StreamTokenizer.TT_EOF != tokenizer.nextToken()) { if (StreamTokenizer.TT_NUMBER == tokenizer.ttype) { System.out.print (tokenizer.nval + " "); fileOut.print(tokenizer.nval + " "); } else if (StreamTokenizer.TT_WORD == tokenizer.ttype) { System.out.print (tokenizer.sval + " "); fileOut.print (tokenizer.sval + " "); } else if (StreamTokenizer.TT_EOL == tokenizer.ttype) { System.out.println (); fileOut.println (); } else { System.out.println ("Other token type: " + tokenizer.ttype + " " + tokenizer.sval + " " + tokenizer.toString ()); fileOut.println ("Other token type: " + tokenizer.ttype + " " + tokenizer.sval + " " + tokenizer.toString ()); } } System.out.println ("\nDone!"); fileOut.close(); // IMPORTANT! } catch (IOException e) { System.out.println(e.getMessage ()); } } /** * @param arg[0] the input file name. * @param arg[1] the output file name. */ public static void main(String arg[]) { new TestStreamTokenizer().testInOut(arg[0], arg[1]); } }