Java Reference
In-Depth Information
The Standard Streams
Your operating system will typically define three standard streams that are accessible through members
of the System class in Java:
A standard input stream that usually corresponds to the keyboard by default. This is
encapsulated by the in member of the System class, and is of type InputStream .
A standard output stream that corresponds to output on the command line. This is
encapsulated by the out member of the System class, and is of type PrintStream .
A standard error output stream for error messages that usually maps to the command line
output by default. This is encapsulated by the err member of the System class, and is also of
type PrintStream .
You can reassign any of these to another stream within a Java application. The System class provides
the static methods setIn() , setOut() , and setErr() for this purpose. The setIn() method
requires an argument of type InputStream that specifies the new source of standard input. The other
two methods expect an argument of type PrintStream .
Since the standard input stream is of type InputStream , we are not exactly overwhelmed by the
capabilities for reading data from the keyboard in Java. Basically, we can read a byte or an array of bytes
using a read() method as standard, and that's it. If you want more than that, reading integers, or decimal
values, or strings as keyboard input, you're on your own. Let's see what we can do to remedy that.
Getting Data From the Keyboard
To get sensible input from the keyboard, you have to be able to scan the stream of characters and
recognize what they are. When you read a numerical value from the stream, you have to look for the
digits and possibly the sign and decimal point, figure out where the number starts and ends in the
stream, and finally convert it to the appropriate value. To write the code to do this from scratch would
take quite a lot of work. Fortunately, we can get a lot of help from the StreamTokenizer class in the
java.io package.
The term token refers to a data item, such as a number or a string that will, in general, consist of several
consecutive characters of a particular kind from the stream. For example, a number is usually a
sequence of characters that consists of digits, maybe a decimal point, and sometimes a sign in front. The
class has the name StreamTokenizer because it can read characters from a stream, and parse it into a
series of tokens that it recognizes.
You create a StreamTokenizer object from a stream reader object that reads data from the
underlying input stream. Since we want to read the standard input stream, System.in , we shall use an
InputStreamReader that converts the raw bytes from the stream from the local character encoding
to Unicode characters before the StreamTokenizer object sees them. In the interests of efficiency we
will also buffer the data from the InputStreamReader through a BufferedReader object that will
buffer the data in memory. We will therefore create our StreamTokenizer object like this:
StreamTokenizer tokenizer = new StreamTokenizer(
new BufferedReader(
new InputStreamReader(System.in)));
Search WWH ::




Custom Search