Skip to content

Instantly share code, notes, and snippets.

@girish3
Last active May 31, 2019 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girish3/7e12ee260957599e32b4c1c9218bc743 to your computer and use it in GitHub Desktop.
Save girish3/7e12ee260957599e32b4c1c9218bc743 to your computer and use it in GitHub Desktop.
[What is InputStream & Output Stream in Java? Why and when do we use them?] #java

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();

Ref:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment