Java Reference
In-Depth Information
Reading Input from the Keyboard (continued)
e.printStackTrace();
}
}
}
Figure 16.7
Sample output of the KeyboardInput program.
Notice the use of the Integer.parseInt() method to parse the String input into ints. Also
note that the readLine() method blocks until the user presses the Enter key on the
keyboard.
Using Pipes
A pipe refers to a stream between threads, which allows for interthread com-
munication. The PipedInputStream and PipedOutputStream classes are used
when the data is bytes, and the PipedReader and PipedWriter classes are used
when working with character data. The constructors for the pipe stream
classes look similar to:
public PipedInputStream().
Creates a new, unconnected pipe input
stream.
public PipedInputStream(PipedOutputStream). Creates a new pipe
input stream that is connected to the given pipe output stream.
public PipedOutputStream().
Creates a new, unconnected pipe output
stream.
public PipedOutputStream(PipedInputStream). Creates a new pipe out-
put stream that is connected to the given pipe input stream.
Creating a pipe is a two-step process. One thread creates a new unconnected
pipe, then a second thread comes along and connects to this existing pipe.
Alternatively, two unconnected pipes can be connected using the connect()
method of the corresponding class:
Search WWH ::




Custom Search