You learned about the traditional approach in Segment 482 and you learned about the token approach in Segment 487.
To see how the more sophisticated approach is related to the traditional
approach, you can study the following definition of MovieStreamTokenizer
,
which is derived from the program shown in Segment 486.
import java.io.*; public class MovieStreamTokenizer { FileInputStream stream; InputStreamReader reader; BufferedReader buffer; Movie movie; int MORE = 0, EOF = 1; public static void main(String argv[]) throws IOException { MovieStreamTokenizer tokens = new MovieStreamTokenizer("input.data"); while(tokens.nextMovie() != tokens.EOF) { Movie m = tokens.movie; System.out.println("Rating: " + m.rating()); } } public MovieStreamTokenizer (String fileName) throws IOException { stream = new FileInputStream("input.data"); reader = new InputStreamReader(stream); buffer = new BufferedReader(reader); } public int nextMovie () throws IOException { String line = buffer.readLine(); if (line != null && !line.equals("")) { line = line.trim(); int nextSpace = line.indexOf(" "); int x = Integer.parseInt(line.substring(0, nextSpace)); line = line.substring(nextSpace).trim(); nextSpace = line.indexOf(" "); int y = Integer.parseInt(line.substring(0, nextSpace)); line = line.substring(nextSpace).trim(); int z = Integer.parseInt(line); movie = new Movie(x, y, z); return MORE; } stream.close(); return EOF; } }
The MovieStreamTokenizer
constructor assigns values to instance
variables for the file input stream, the input-stream reader, and the
buffered reader. Each call to nextMovie
reads a line of text from
the buffered reader, processes that line to produce the arguments needed by
the Movie
constructor, and returns the value of the MORE
instance variable. If there are no more lines of text, then
nextMovie
returns the value of the EOF
variable.
Thus, a MovieStreamTokenizer
instance provides a stream of movie
instances, accessible via the movie
instance variable, just as an
ordinary stream tokenizer provides a stream of strings and doubles,
accessible via the nval
and sval
instance variables.