// Here's an example program that opens a file and then reads (and prints)
// strings from it until end-of-file is reached.
//     This program uses a FileInputStream, whose meaning is fairly obviousl.
// But it turns that FileInputStream into a StreamTokenizer.  A StreamTokenizer
// is a stream that can be pulled apart, piece by piece.  Each piece is called
// a "token".  If you wish, you can specify what are the separating characters
// that define where the tokens begin and end.  We haven't used that feature
// here.


import java.io.*;

class Tester {


    // Opens an input file with the given `name', and returns a StreamTokenizer
    // for it.  If the file doesn't exist, returns the null StreamTokenizer.
    // ------------------------------------------------------------------------
    public static StreamTokenizer openInputFile (String name) {
            try {
		// Try to open the file.
                FileInputStream fs = new FileInputStream(name);
		// If that worked, convert the file to a StreamTokenizer.
                return new StreamTokenizer(fs);
            } catch (FileNotFoundException e) {
		// We were unable to open the file, so return null.
                return null;
            }
    }


    public static void main (String[] args) throws IOException {

	 // Read in the name of a file.
         DataInputStream stdin = new DataInputStream(System.in);
         System.out.println("Please tell me what file to read from: ");
         String filename = stdin.readLine();

	 // Try to open that file.  
         StreamTokenizer st = openInputFile(filename);
         if (st == null)
	    // Didn't work -- give up.
	    System.out.println("Unable to open file");
         else {
	    // The file is open.  Now read and print strings from it
	    // until end-of-file is reached.
	    String s; 
	    // Figure out what the next "token" is in the stream.
	    st.nextToken();
 	    // If that token's type isn't end-of-file, keep going.
	    while (st.ttype != st.TT_EOF) {
		// Grab that token's string value and print it.
                s = st.sval;
	        System.out.println("Read token: " + s);
		// Then figure out what the next token is.
	        st.nextToken();
            }

         }

    }

}
