Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Someone explain to me what InputStream and OutputStream are?

I am confused about the use cases for both InputStream and OutputStream.

If you could also include a snippet of code to go along with your explanation, that would be great. Thanks!

like image 218
Bohemian Avatar asked Dec 02 '09 04:12

Bohemian


People also ask

What are InputStream in Java?

InputStream class is the superclass of all the io classes i.e. representing an input stream of bytes. It represents input stream of bytes. Applications that are defining subclass of InputStream must provide method, returning the next byte of input.

Why do we need InputStream in Java?

The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination. Here is a hierarchy of classes to deal with Input and Output streams.

What is InputStream from a file?

FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader . Since: JDK1.0. See Also: File , FileDescriptor , FileOutputStream , Files.


2 Answers

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(); 
like image 82
Chip Uni Avatar answered Oct 03 '22 10:10

Chip Uni


InputStream is used for reading, OutputStream for writing. They are connected as decorators to one another such that you can read/write all different types of data from all different types of sources.

For example, you can write primitive data to a file:

File file = new File("C:/text.bin"); file.createNewFile(); DataOutputStream stream = new DataOutputStream(new FileOutputStream(file)); stream.writeBoolean(true); stream.writeInt(1234); stream.close(); 

To read the written contents:

File file = new File("C:/text.bin"); DataInputStream stream = new DataInputStream(new FileInputStream(file)); boolean isTrue = stream.readBoolean(); int value = stream.readInt(); stream.close(); System.out.printlin(isTrue + " " + value); 

You can use other types of streams to enhance the reading/writing. For example, you can introduce a buffer for efficiency:

DataInputStream stream = new DataInputStream(     new BufferedInputStream(new FileInputStream(file))); 

You can write other data such as objects:

MyClass myObject = new MyClass(); // MyClass have to implement Serializable ObjectOutputStream stream = new ObjectOutputStream(     new FileOutputStream("C:/text.obj")); stream.writeObject(myObject); stream.close(); 

You can read from other different input sources:

byte[] test = new byte[] {0, 0, 1, 0, 0, 0, 1, 1, 8, 9}; DataInputStream stream = new DataInputStream(new ByteArrayInputStream(test)); int value0 = stream.readInt(); int value1 = stream.readInt(); byte value2 = stream.readByte(); byte value3 = stream.readByte(); stream.close(); System.out.println(value0 + " " + value1 + " " + value2 + " " + value3); 

For most input streams there is an output stream, also. You can define your own streams to reading/writing special things and there are complex streams for reading complex things (for example there are Streams for reading/writing ZIP format).

like image 23
Arne Deutsch Avatar answered Oct 03 '22 09:10

Arne Deutsch