Introduction Of I/O (Input and Output) In Java
I/O (Input and Output)
Input and output is used to process the input and generate the output, based on the input in Java.
In Java, we use the concept of stream to make input and output process fast.
The java.io package contains all the classes which we required for input and output operations.
We can do file handling by Java IO API.
Stream
A stream is a sequence of data and it is composed of bytes in Java.
Three streams are created automatically and all are attached with console.
- System.out: It is a standard output stream.
- System.in: It is a standard input stream.
- System.err: It is a standard error stream.
OutputStream
The OutputStream is used to write the data to a destination.
InputStream
The InputStream is used to read the data from a source.
Let's understand with the figure, given below.
OutputStream class
OutputStream class is the parent class of all classes representing an output stream of bytes and it is an abstract class. An output stream accepts output bytes and sends them.
Methods of OutputStream class
public void write(int)throws IOException
This method is used to write a byte to the current output stream.
public void write(byte[])throws IOException
This method is used to write an array of byte to the current output stream.
public void flush()throws IOException
This method is used to flush the current output stream.
public void close()throws IOException
This method is used to close the current output stream.
Hierarchy of classes to deal with Output streams.
InputStream class
InputStream class is the parent class of all the classes representing an input stream of bytes and it is an abstract class.
Methods of InputStream class
public abstract int read()throws IOException
This method is used to read the next byte of the data from the input stream. It returns -1 at the end of file.
public int available()throws IOException
This method is used to return an estimate of the number of bytes that can be read from the current input stream.
public void close()throws IOException
This method is used to close the current input stream.
Hierarchy of classes to deal with Input streams.
Summary
Thus, we learnt that input and output is used to process the input and generate the output, based on the input in Java.