Sunday, 24 April 2011

What is stream in java IO?

Stream
A stream is a sequence of data of undetermined length. It's called a stream because it's like a stream of water that continues to flow. There's no definite end to it.

A better analogy might be a queue of people waiting to get on a ride at an amusement park. As people are processed at the front (i.e. get on the roller coaster) more are added at the back of the line. If it's a slow day the roller coaster may catch up with the end of the line and have to wait for people to board. Other days there may always be people in line until the park closes. Each person is a discrete individual and must be put on the roller coaster or not put on the roller coaster. There are no half-people. There's always a definite number of people in line though this number may change from moment to moment as people enter at the back of the line and exit from the front of the line. Although all the people are discrete, you'll sometimes have a family that must be put together in the same car. Thus although the individuals are discrete, they aren't necessarily unrelated.

In Java a stream is composed of discrete bytes. The bytes may represent chars or other kinds of data. They may come faster than you can handle them, or your thread may block while waiting for the next one to arrive. It often doesn't matter.
The key to processing the stream is a while loop that processes each piece of data, until you encounter the end-of-stream character or some other exceptional condition occurs. In Unix you type end-of-stream with Control-D. On Windows, you type end-of-stream with Control-Z.
 

The Stream Classes


 

No comments:

Post a Comment