What is InputStream & Output Stream? Why and when do we use them?

前端 未结 8 941
予麋鹿
予麋鹿 2020-11-29 14:09

Someone explain to me what InputStream and OutputStream are?

I am confused about the use cases for both InputStream and

相关标签:
8条回答
  • 2020-11-29 15:00

    OutputStream is an abstract class that represents writing output. There are many different OutputStream classes, and they write out to certain things (like the screen, or Files, or byte arrays, or network connections, or etc). InputStream classes access the same things, but they read data in from them.

    Here is a good basic example of using FileOutputStream and FileInputStream to write data to a file, then read it back in.

    0 讨论(0)
  • 2020-11-29 15:01

    you read from an InputStream and write to an OutputStream.

    for example, say you want to copy a file. You would create a FileInputStream to read from the source file and a FileOutputStream to write to the new file.

    If your data is a character stream, you could use a FileReader instead of an InputStream and a FileWriter instead of an OutputStream if you prefer.

    InputStream input = ... // many different types
    OutputStream output = ... // many different types
    
    byte[] buffer = new byte[1024];
    int n = 0;
    while ((n = input.read(buffer)) != -1)
        output.write(buffer, 0, n);
    
    input.close();
    output.close();
    
    0 讨论(0)
  • 2020-11-29 15:08

    The goal of InputStream and OutputStream is to abstract different ways to input and output: whether the stream is a file, a web page, or the screen shouldn't matter. All that matters is that you receive information from the stream (or send information into that stream.)

    InputStream is used for many things that you read from.

    OutputStream is used for many things that you write to.

    Here's some sample code. It assumes the InputStream instr and OutputStream osstr have already been created:

    int i;
    
    while ((i = instr.read()) != -1) {
        osstr.write(i);
    }
    
    instr.close();
    osstr.close();
    
    0 讨论(0)
  • 2020-11-29 15:10

    An output stream is generally related to some data destination like a file or a network etc.In java output stream is a destination where data is eventually written and it ends

    import java.io.printstream;
    
    class PPrint {
        static PPrintStream oout = new PPrintStream();
    }
    
    class PPrintStream {
        void print(String str) { 
            System.out.println(str)
        }
    }
    
    class outputstreamDemo {
        public static void main(String args[]) {
            System.out.println("hello world");
            System.out.prinln("this is output stream demo");
        }
    }
    
    0 讨论(0)
  • 2020-11-29 15:12

    A stream is a continuous flow of liquid, air, or gas.

    Java stream is a flow of data from a source into a destination. The source or destination can be a disk, memory, socket, or other programs. The data can be bytes, characters, or objects. The same applies for C# or C++ streams. A good metaphor for Java streams is water flowing from a tap into a bathtub and later into a drainage.

    The data represents the static part of the stream; the read and write methods the dynamic part of the stream.

    InputStream represents a flow of data from the source, the OutputStream represents a flow of data into the destination. Finally, InputStream and OutputStream are abstractions over low-level access to data, such as C file pointers.

    0 讨论(0)
  • 2020-11-29 15:13

    Stream: In laymen terms stream is data , most generic stream is binary representation of data.

    Input Stream : If you are reading data from a file or any other source , stream used is input stream. In a simpler terms input stream acts as a channel to read data.

    Output Stream : If you want to read and process data from a source (file etc) you first need to save the data , the mean to store data is output stream .

    0 讨论(0)
提交回复
热议问题