Java IO Tutorial Introduction

Java I/O (Input and Output) is basically used to process the input and produce the output . whenever we write any program it takes some input , process it and then produces an ouput . So Java IO helps us to take an input inside our Java program and to produce output from Java program.

  Java uses the concept of stream to make I/O operation fast.

  The java.io package contains all the classes required for input and output operations.


Streams in Java

A stream is a sequence of flow of data . In Java a stream is composed of bytes. In Java, Stream is a channel or a path along which data flows between source and destination.There are two kinds of streams -

  Input Stream - Input stream is a path through which data is read or fetched from a source(file, memory, or a console).

  Output Stream - Output Stream is a path which is used to write some data to a destination(file, memory or a console).

Based on the type of data that we want to read, Java has two Stream classes - Byte Stream Classes and Character Stream Classes

Byte Stream Classes

Byte Stream Classes are used to read bytes from an input stream and write bytes to an output stream.

  InputStream Classes - These classes are subclasses of an abstract class, InputStream and they are used to read bytes from a source(file, memory or console).

  OutputStream Classes - These classes are subclasses of an abstract class, OutputStream and they are used to write bytes to a destination(file, memory or console).

InputStream

InputStream class is a base class of all the classes that are used to read bytes from a file, memory or console. InputStream is an abstract class and hence we can't create its object but we can use its subclasses for reading bytes from the input stream. We will discuss subclasses of InputStream in the next few articles with examples.

Methods Description
abstract int read() This method reads the next byte out of the input stream.
int read(byte[] b) This method reads a chunk of bytes from the input stream and store them in its byte array, b.
int available() This method returns the number of bytes that can be read from the input stream.
close() This method closes this output stream and also frees any resources connected with this output stream.

  Above are the methods of InputStream class. These methods are inherited by all the sub class of InputStream class .

OutputStream

OutputStream class is a base class of all the classes that are used to write bytes to a file, memory or console. OutputStream is an abstract class and hence we can't create its object but we can use its subclasses for writing bytes to the output stream. In the diagram below we have shown the hierarchy of OutputStream class and some of its important subclasses that are used to write bytes.

Methods Description
public void write(int)throws IOException This method writes byte(contained in an int) to the output stream.
public void write(byte[])throws IOException This method writes a whole byte array(b) to the output.
public void flush()throws IOException This method flushes the output steam by forcing out buffered bytes to be written out.
public void close()throws IOException It is used to close the current output stream.

  Above are the methods of OutputStream class. These methods are inherited by all the sub class of OutputStream class .