go to previous page   go to home page   go to next page

Answer:

Yes. For example, a text editor typically does output to both the monitor and to a disk file. Its input comes from both a disk file and the keyboard.


Processing Streams

connected streams

A processing stream operates on the data supplied by another stream. Often a processing stream acts as a buffer for the data coming from another stream. A buffer is a block of main memory used as a work area. For example, disks usually deliver data in blocks of 512 bytes, no matter how few bytes a program has asked for. Usually the blocks of data are buffered and delivered from the buffer to the program in the amount the program asked for.

In the picture, the keyboard sends data to the InputStream System.in which is connected to a InputStreamReader stream which is connected to a BufferedReader stream. System.in is a stream object that the Java system automatically creates when your program starts running.

The data are transformed along the way. The raw bytes from the keyboard are grouped together into a String object that the program reads using stdin.readLine().

A program can set all this up by declaring a BufferedReader as follows:

BufferedReader stdin = 
  new BufferedReader( 
    new  InputStreamReader( System.in ) );

This may seem like an unnecessary complication. But java.io gives you a collection of parts that can be assembled to do nearly any I/O task you need.



QUESTION 4:

(Thought Question: ) Might several connected streams be used for output?