Improving wc( ) Using a streamtokenizer
An even better way to-look for patterns in an input stream is to use another of Java’s 1/0 classes: StreamTokenizer. Similar to StringTokenizer from Chapter 16, StreamTokenizer breaks up the InputStream into tokens that are delimited by sets of characters. It has this constructor:
We use the eolIsSignificant() method to ensure that newline characters will be delivered as tokens, so we can count the number of lines as well as words. It has this · general form:
The wordChars( ) method is used to specify the range of characters that can be used in words. Its general form is shown here
Here, start and end specify the range of valid characters. In the program, characters in the range 33 to 255 are valid word characters.
The whitespace characters are specified using whH space Chars(). It has this .general form:
StreamTokenizer defines four int constants: TT_EOF, IT_EOL, n_NUMBER, and IT_WORD. There are three instance variables. nivalis public double used to hold the’values of numbers as they are recognized. sval is a public String used to hold the value of ilny words as they are recognized type is a public int indicating the type of token that has just been read by the nextToken ) method. If the token is a word, type equals WORD. If the token is a number, type equals IT_NUMBER. If the token is a single character! Hype contains its value. If an end-of-line condition has been encountered, type equals ‘IT_EOL. (This assumes that colIs Significant( ) was invoked with a true argument.) If the end of the stream has been encountered, type equals.
Serialization is the process of writing the state of an object to a byte stream. This is useful “when you want to save the state of your program to a persistent storage area, such as a file. A.ta later time, you may restore these objects by using the process of deserialization
Assume that an object to be serialized has references to other objects, which, in turn, have references to still more objects. This set of objects and the relationships among them form a directed graph, There may also be circular references within this . object graph. That is, object X may containareference to object Y, and object Y may’ contain a reference back to object X. Objects n1ay also contain references to themselves. The object serialization and deserialization facilities have been designed to work . correctly in these sceneries. If you attempt to serialize an object at the top of an object graph, all of the other referenced objects are recursively located and serialized. Similarly,.during theprocess of deserialization, an of these objects and their references are correctly restored.
An overview of the interfaces and classes that support serialization follows.