import java.util.StringTokenizer; import java.io.*; /** * StringTok3 --- A simple program to show the String Tokenizer using File Input * @author Matthew Sharritt */ public class StringTok3 { /** * Reads a File and shows the separate tokens, then the number of * tokens left- one line at a time * @param arg A string array containing the command line arguments. * @return No return value. * @exception Exception If the file is not found or other error. */ public static void main (String[] arg) throws Exception { try{ File f = new File("input.txt"); // code to open a file... FileInputStream fis = new FileInputStream(f); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String s = ""; int counter = 1; // counter for number of lines in the file StringTokenizer st; // declare the String Tokenizer here s = br.readLine( ); // read the first line of the file while(s != null){ // for each line of the File... st = new StringTokenizer(s, "#"); // separate based on a # while (st.hasMoreTokens()) { // for each token in the line System.out.print(st.nextToken()); System.out.print("\t\t"); System.out.println("Number of Tokens Left in line " + counter + " of the file: " + st.countTokens()); } System.out.println("-----------------------------"); s = br.readLine( ); // read the next line of the File counter++; } } catch(FileNotFoundException fnfe){ System.out.println("File not found"); // if input file is missing... } } }