How is input stream different from output stream?

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

A comment. Please do not close this question. This is the main kind of material that can mix people in interviews.

UPDATE 1 - It seems everyone is saying the same thing - you read from IS and write to the OS. So they are basically the same. Like a pipe with running water. When you use water from this pipe, you call it InputStream and when you pour water into it, it is called the output stream. Is this really trivial?

UPDATE2 - If the difference is not so great, then can we have an InAndOutStream instead of making code for two classes (InputStream and OutputStream)?

+6
source share
7 answers

They are conceptually different.

  • from InputStream you read
  • to OutputStream you write
+15
source

A stream is data that you access sequentially. You might think of it as the train that you are watching from the entrance to the tunnel so that you can only see one car at a time. Or a stream of widgets passing through a conveyor belt, requiring you to tighten the screw on each of them, before it moves on to the next person on the conveyor, who must fill it with a hammer and so on. Or sticks floating on the river while you look from the bridge.

Regardless of how they work internally, all threads represent the same simple model for programs that use them: a thread is a sequence of data. The program uses the input stream to read data from the source, one element at a time: Io stream

The program uses the output stream to write data to the destination, one element at a time: Io stream

+4
source

If the difference is not so great, then we have InAndOutStream instead of making code for two classes (InputStream and OutputStream)?

This is not possible, with the exception of the ByteArray classes. The reason is that most threads are supported by system devices such as a file or socket. In the case of a file, the file you are writing is the file you are reading, but in the case of a socket, the stream you are writing does not depend on the stream you are reading.

Even an attempt to combine them would be a bad idea IMHO, as people are embarrassed between text and binary streams. If they could read and write in the same stream, you would become even more confused, I suspect.

In general, developers should have a clear idea of โ€‹โ€‹whether they are read or written from the device. If they do not understand this, you will have problems.

FYI: I wrote a library that does exactly what you offer in the memory stream, for example, "pipe";) However, I would say that this is only for experienced developers, and even then I keep the interfaces as clear as possible to prevent confusion.

+4
source

You read with an InputStream , and you write in an OutputStream . This is the key difference between the two.

In some cases, you should be able to read and write. One such example is a socket stream. The way the Java library is processed is to have an InputStream and an OutputStream .

+2
source

The concept of threads is not as symmetrical as you mean in your question. Since this is Java, the reader and writer of the stream occupy radically different positions in the runtime: one implements the stream object, and the other calls methods on it.

Therefore, since linguistic power is asymmetric in the model, the developer of any input / output object in Java, it is necessary to determine in advance whether to occupy the reading side or the written side of the stream. Object users do not receive a selection. For example, then; in order to implement an input / output facility that receives data from its users, it is necessary to implement an OutputStream (to which users would have to write).

To implement a symmetrical tool, the language must be capable of collaborative execution. In this case, the threads may indeed have a readable and writable side, and the caller trying to write will succumb to the reader, and vice versa. However, Java does not support such features. (At least beyond multithreading, which will obviously be too difficult for most situations.)

I hope this becomes more clear.

+2
source

Itโ€™s best to keep them small when the user is just about to read the stream, why load the functions that are used to write to the stream? This is based solely on my guess, though ...

edit: I was stupid with the word file, threads can be used for more files.

0
source

To avoid confusion between the stream, for example the html registration form, the data entering the form is called the input data, as soon as the registration is successful, or the failure output is known as out-data. In the same way, the application receives data through the input stream, the application sends data through the output stream.

The following code helps to avoid confusion between threads:

 //Standard input: Scanner scan = new Scanner(System.in); String s = scan.next(); //Standard output System.out.println("Hello World!"); 
0
source

All Articles