Java Reference
In-Depth Information
}
catch(IOException e) {
e.printStackTrace();
}
}
}
String value: This
String value: is
String value: a
String value: test
Number value: 200.89
String value: which
String value: is
String value: simple
Number value: 50.0
The program uses a StringReader object as the data source. You can use a FileReader object or any other
Reader object as the data source. The syntax to get the tokens is not easy to use. The nextToken() method of
StreamTokenizer is called repeatedly. It populates three fields of the StreamTokenizer object: ttype , sval , and nval .
The ttype field indicates the token type that was read. The following are the four possible values for the ttype field:
TT_EOF: End of the stream has been reached.
TT_EOL: End of line has been reached.
TT_WORD: A word (a string) has been read as a token from the stream.
TT_NUMBER: A number has been read as a token from the stream.
If the ttype has TT_WORD , the string value is stored in its field sval . If it returns TT_NUBMER , its number value is
stored in nval field.
StreamTokenizer is a powerful class to break a stream into tokens. It creates tokens based on a predefined syntax.
You can reset the entire syntax by using its resetSyntax() method. You can specify your own set of characters that
can make up a word by using its wordChars() method. You can specify your custom whitespace characters using its
whitespaceChars() method.
Summary
Reading data from a data source and writing data to a data sink is called input/output. A stream represents a data
source or data sink for serial reading or writing. The Java I/O API contains several classes to support input and output
streams. Java I/O classes are in the java.io and java.nio packages. The input/output stream classes in Java are based
on the decorator pattern.
You refer to a file in your computer by its pathname. A file's pathname is a sequence of characters by which
you can identify it uniquely in a file system. A pathname consists of a file name and its unique location in the file
system. An object of the File class is an abstract representation of a pathname of a file or a directory in a platform-
independent manner. The pathname represented by a File object may or may not exist in the file system. The File
class provides several methods to work with files and directories.
Java I/O supports two types of streams: byte-based streams and character-based streams. Byte-based streams
are inherited from the InputStream or OutputStream classes. Character-based stream classes are inherited from the
Reader or Writer classes.
 
Search WWH ::




Custom Search