Processing Streams

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
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

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, usually disks deliver their 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 to the program in the amounts it asks for.

In the following, 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.

connected streams

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 IO task you need.

QUESTION 4:

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