/* FileWordList.java */ import java.io.*; /** Implements a class for reading a words (as Strings) from a file. */ public class FileWordList { /** Constructs a new FileWordList. * @param `fname' is the file name to be used for reading words. * @exception FileNotFoundException is thrown in the filename does * does not exist. */ public FileWordList( String fname ) throws FileNotFoundException { filename = fname; reset(); } /** Re-opens the file for reading from the beginning. * @exception FileNotFoundException is thrown in the filename does * does not exist. */ public void reset() throws FileNotFoundException { FileInputStream f = new FileInputStream( filename ); InputStreamReader i = new InputStreamReader( f ); Reader r = new BufferedReader( i ); fileStream = new StreamTokenizer( r ); } /** Checks whether more input exists. * @return true <==> no more input waiting. */ public boolean isEOF() { return (fileStream.ttype == StreamTokenizer.TT_EOF); } /** Read the next word from the file. * @return the next word from the file, or null if no more words * are available. */ public String nextWord() throws IOException { if( isEOF() ) return null; fileStream.nextToken(); if( isEOF() ) return null; return fileStream.sval; } private String filename; private StreamTokenizer fileStream; }