Header Ads Widget

Java I/O Streams

Java I/O Streams

In Java, streams are the sequence of data that are read from the source and written to the destination.

An input stream is used to read data from the source. And, an output stream is used to write data to the destination.

class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!"); 

    }

}

For example, in our first Hello World example, we have used System.out to print a string. Here, the System.out is a type of output stream.

Similarly, there are input streams to take input.

Input stream reads data from source to program and output stream writes file from program to destination

We will learn about input streams and output streams in detail in the later tutorials.

  

Before exploring various input and output streams lets look at 3 standard or default streams that Java has to provide which are also most common in use:

  1. System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
  2. System.out: This is the standard output stream that is used to produce the result of a program on an output device like the computer screen.
  3. System.err: This is the standard error stream that is used to output all the error data that a program might throw, on a computer screen or any standard output device.

Types of Streams

Depending upon the data a stream holds, it can be classified into:

  • Byte Stream
  • Character Stream

Byte Stream

Byte stream is used to read and write a single byte (8 bits) of data.

All byte stream classes are derived from base abstract classes called InputStream and OutputStream.

InputStream: 

InputStream is an abstract class of Byte Stream that describe stream input and it is used for reading and it could be a file, image, audio, video, webpage, etc. it doesn’t matter. Thus, InputStream read data from source one item at a time.

Input Stream in Java

OutputStream: 

OutputStream is an abstract class of Byte Stream that describes stream output and it is used for writing data to a file, image, audio, etc. Thus, OutputStream writes data to the destination one at a time.

OutputStream in Java

Difference between InputStream and OutputStream

InputStream

OutputStream

1. It is an abstract class that describes Stream Input.

1. It is an abstract class that describes Stream Output.

2. InputStream Read data from the source once at a time.

2. OutputStream Write Data to the destination once at a time.

3. InputStream consist of method which performs:

  • Read next byte of data from the input stream and return -1 at the end of the file: public abstract int read()throws IOException
  • Close current InputStream: public int available()throws IOException
  • Returns an estimate of the number of bytes that can be read from the current input stream: public void close()throws IOException

3. Output Stream consists of methods which perform:

  • Write a byte to current Outputstream : public void write(int)throws IOException
  • Write array of byte to current output stream : public void write(byte[])throws IOException
  • Flushes the current OutputStream: public void flush()throws IOException
  • Close  current Output Stream. : public void close()throws IOException

4. Types of InputStream are:

  • FileInputStream
  • ByteArrayInputStream
  • FilterInputStream
  • ObjectInputStream

In these types the most important and mostly used type is FileInputStream.



 

4. Types of OutputStream are:

  • FileOutputStream
  • ByteArrayOutputStream
  • FilterOutputStream
  • ObjectOutputStream

In these types the most important and mostly used type is FileOutput Stream.

 

Character Stream

In Java, characters are stored using Unicode conventions (Refer this for details). Character stream automatically allows us to read/write data character by character. Though it has many classes, the FileReader and the FileWriter are the most popular ones. FileReader and FileWriter are character streams used to read from the source and write to the destination respectively.

Stream class

Description

BufferedReader

It is used to handle buffered input stream.

FileReader

This is an input stream that reads from file.

InputStreamReader

This input stream is used to translate byte to character.

OutputStreamReader

This output stream is used to translate character to byte.

Reader

This is an abstract class that define character stream input.

PrintWriter

This contains the most used print() and println() method

Writer

This is an abstract class that define character stream output.

BufferedWriter

This is used to handle buffered output stream.

FileWriter

This is used to output stream that writes to file.


Post a Comment

0 Comments