Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is an Input Stream different from an Output Stream?

I see that both are "streams" of data. In that case, why consider them different ? What is the difference really ?

Comment - Please don't close this question. It is the basic kind of stuff that can confound people in an interview.

UPDATE 1 - Everyone seems to say the same thing - you read from IS and write into OS. So, they are basically the same. Like a pipe with water flowing through it. When you use the water from that pipe you call it InputStream and When you pump water into it, its called an output stream. Is it really that trivial ?

UPDATE2 - If the difference is not so "big", then can we have a InAndOutStream instead of having to make code for two classes (InputStream and OutputStream) ?

like image 392
david blaine Avatar asked Dec 17 '12 07:12

david blaine


2 Answers

They are conceptually different

  • from InputStream you read
  • to OutputStream you write
like image 88
Nikolay Kuznetsov Avatar answered Sep 30 '22 03:09

Nikolay Kuznetsov


A stream is data that you access in sequence. You could think of it like a train that you watch from a tunnel entrance so that you can just see one car at a time. Or a stream of widgets coming across a conveyor belt requiring you to tighten a screw on each one before it passes by to the next person down the assembly line who has to pound it with a hammer, and so on. Or sticks floating down a river while you watch from a bridge.

No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data. A program uses an input stream to read data from a source, one item at a time: IO Stream

A program uses an output stream to write data to a destination, one item at time: IO Stream

like image 32
Subhrajyoti Majumder Avatar answered Sep 30 '22 02:09

Subhrajyoti Majumder