简体   繁体   中英

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) ?

They are conceptually different

  • from InputStream you read
  • to OutputStream you write

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流

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

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) ?

This is not possible with the exception of a the ByteArray classes. The reason is that most Streams are backed by system provided devices such as a file or a socket. In the case of a file, the file you write is the file you read from but in the case of a socket, the stream you write to is independant of the stream you read from.

Even attempting to combine them would be a bad idea IMHO as people get confused enough between text and binary streams. If they could read and write to the same stream you would get even more confusion I suspect.

In general, developers should have a clear idea as to whether they are read or writing from a device. If that is not clear to them you are bound to have problems.

FYI: I have written a library which does exactly what you suggest for in memory stream like "pipes" ;) However, I would say it is only for expert developers and even then I keep the interfaces as distinct as possible to prevent confusion.

You read from an InputStream , and you write to an OutputStream . That's the key difference between them.

In some cases you need to be able to both read and write. One such example is a stream socket . The way the Java library handles this is by having an InputStream and an OutputStream .

The concept of the streams are not as symmetrical as you imply in your question. Since this is Java, the reader and writer of the stream occupy radically different positions in the execution environment: One implements the stream object, and the other calls methods on it.

Therefore, since the language forces asymmetry on the model, an implementer of any I/O facility in Java, then, needs to determine in advance whether to occupy the reading side or the writing side of the stream. Users of the facility don't get the choice. For instance, then; in order to implement an I/O facility that receives data from its users, one would need to implement an OutputStream (which users would call upon to write to).

To implement a symmetrical facility, the language would need to be capable of co-routine execution. In that case, streams could truly have a readable and a writable side, and a caller trying to write would yield to the reader, and vice versa. Java does not support such facilities, however. (At least outside of multi-threading, which would clearly be far too heavy-weight for most situations.)

I hope that makes it a bit more clear.

My best bet is to keep them small, when a user is only going to read a stream, why load functions that are used to write to a stream? This is based purely on my guess though...

edit: I was being silly with the word file, streams can be used for more than files.

To avoid confusion between the stream, for example html registration form, the data enter into the form is known as input-data, once the registration is success or failure output is known as out-data. In the same manner an application receives data via input-stream, an application sends data via output-stream.

The following code helps us to avoid confusion between streams:

//Standard input: 
Scanner scan = new Scanner(System.in);
String s = scan.next();

//Standard output
System.out.println("Hello World!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM