// 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 obvious.
// 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.*;
import java.awt.*;

class Tester {


    // Ask the user for and open an input file, and return a StreamTokenizer
    // for it.  If the file doesn't exist, return the null StreamTokenizer.
    // ------------------------------------------------------------------------
    public static StreamTokenizer openInputFile () {
        try {
            // Use a file dialog to ask the user to identify a file.
            Frame f = new Frame();
            FileDialog fd = new FileDialog(f, null, FileDialog.LOAD);
            fd.show();
            String name = fd.getDirectory() + fd.getFile();
        
            // 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 {

         // Try to open that file.  
         StreamTokenizer st = openInputFile();
         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();
            }
         }
    }
}
