☰ Topics

I/O Stream

Taking input in Java: There are 4 ways to take input in console mode.

  1. System.in: It takes input in int and char datatype.
  2. Command Line input: At runtime we need to provide input separated by space. It will take input in String format.
  3. I/O Package: InputStreamReader and BufferedReader class of I/O Package takes input in standard form (String).
  4. util Scanner: It belongs to Scanner class. It has specific datatype input methodology.

For example input a name from the user through Command Line and display it on the console:

class cmd{
    public static void main(String args[]){
        try{
            String s = args[0];
            System.out.print("Your name is: " + s);
        }
        catch(Exception ex){
            System.out.println(ex.toString());
        }
    }
 }
  
While executing this file pass an argument, separated by space. For example, on command prompt write java cmd Rahul.

OUTPUT:

E:\java\new_java>java cmd Rahul
Your name is: Rahul
        
Java performs I/O operations through Streams. A stream is a sequence of data or continuous flow of bits/bytes like a stream of water, travelling from source to destination. Generally it is used to transfer data in network. I/O package has two modes to read and write data. Streams are used because stream means bytes, bytes means ASCII code, ASCII code means binary and computer understands binary.


java.lang package defines a class called System, which encapsulates several aspects of the run-time environment. System contains 3 pre-defined stream variables: in, out and err.
  1. System.out refers to Standard Output Stream, by default this is console.
  2. System.in refers to Standard Input Stream, which is keyboard by default.
  3. System.err refers to Standard Error Stream which is also the console by default.
Java implements streams within class hierarchies defined in the java.io package. I/O package is divided into two streams given as follows:-
  1. Byte Stream:- It provide a rich environment for handling byte-oriented I/O. It can be used with any type of object,including binary data.This versatility makes byte streams important to many types of programs.
  2. Character Stream:- While the ByteStream classes provide sufficient functionality to handle any type of I/O operation, they cannot work directly with Unicode characters.
    since one of the main purpose of Java is to support the "Write Once, Run Anywhere". It is necessary to include direct I/O support for characters.

Byte Stream Classes: Byte stream are defined by using two class hierarchies. At the top are two abstract classes Input Stream and Output Stream. Each of these abstract classes has several concrete subclasses that handle the differences between various devices, such as disk files, network connections and even by memory buffers.
Some important Byte Stream classes:

Stream class Description
BufferedInputStream Used for Buffered Input Stream.
BufferedOutputStream Used for Buffered Output Stream.
DataInputStream Contains method for reading java standard datatype
DataOutputStream Contains method for writing java standard datatype
FileInputStream Reads from a file
FileOutputStream Write to a file
InputStream Abstract class that describe stream input
OutputStream Abstract class that describes stream output
PrintStream Output Stream that contain print() and println() method
Character Stream Classes: Character streams are defined by using two class hierarchies. At the top are two abstract classes Reader and Writer. These abstract classes handle unicode character streams. Java has several concrete subclasses of each of these.
Some important Character Stream classes:
Stream class Description
BufferedReader Handles buffered input stream.
BufferedWriter Handles buffered output stream.
FileReader Input stream that reads from file
FileWriter Output stream that writes to file
InputStreamReader Input stream that translate byte to character
OutputStreamReader Output stream that translate character to byte
PrintWriter Output Stream that contain print() and println() method
Reader Abstract class that define character stream input
Writer Abstract class that define character stream output
Reading Console Input: In java, console input is accomplished by reading from System.in. To obtain a character based stream that is attached to the console, wrap System.in in a BufferedReader object. BufferedReader supports a buffered input stream. System.in refers to an object of type InputStream, it can be used with InputStream. For reading character, read() function is used. For reading string, readLine() function is used. By using System.in and Command Line input, input can be read only from one channel, i.e. Keyboard, not other whereas by using classes of I/O package i.e. InputStreamReader and BufferedReader, input can be read from different channels like keyboard, socket etc. Socket refers to different terminals connected to a server. Therefore, these classes of I\O package are called Standard I\O. When we write 'import java.io.*;' all classes subclasses and interfaces of java.io package is called whereas its packages and sub-packages are not called.

Note: It is compulsory to handle exceptions while performing I\O operations.

For example input a name from the user and display it on the console:
import java.io.*;
class exRead{
    public static void main(String args[]){
        try{
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            System.out.println("Enter your name:");
            String s = br.readLine();
            System.out.print("Your name is: " + s);
        }
        catch(Exception ex){
            System.out.println(ex.toString());
        }
    }
 }
  

OUTPUT:

E:\java\new_java>java exRead
Enter your name:Rahul
Your name is: Rahul