The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Java Input / Output

To read data from keyboard/console, we need to use standard Java classes. These classes are a part of java.io package and provides APIs to perform input/output and exception handling. We will learn about packages later but for the time being you can just remember that a package is a collection of standard Java classes. Exception Handling is an erroneous situation which might occur while performing an operation. At present, we are bothered about exceptions that might occur during input/output. There is dedicated section on Exception Handling later in the tutorial. Right now, we will just see how to import a standard class or an entire package in a Java program and the basic syntax of exception handling while performing I/O. There are a number of ways to take input from keyboard. We will discuss one by one.

BufferedReader and InputStreamReader classes
Java uses the concept of stream to perform I/O. A stream is composed of sequence of bytes. System.in is the standard input stream provided by Java to read a stream from input device. InputStreamReader class is used to convert the byte-oriented stream to a character-oriented stream. BufferedReader class is used to read the data line by line using the readLine( ) method. A single character can be read using read( ) method.
Following program demonstrates how to read a string or a single character from keyboard :

import java.io.*;  // import the package
public class ReadStringDemo {
   public static void main(String args[])throws IOException {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      System.out.println("What is Your Name ?");
      String name = br.readLine(); // take a string as input from keyboard
      System.out.print("What is YourGender ( M/F ) ?");
      char gender = (char)br.read(); // take a character as input
      System.out.print("Do you like Java ( Y/N ) ?");
      char like_java = (char)System.in.read(); // another way to read a char
      System.out.println("Hello " + name);
      System.out.println("Gender : " + gender);
      System.out.println("Like Java : " + like_java);
   }
}
Run this program in your system to take input at run-time  

How to read data of type int, float, etc ?
We need to convert the string read from keyboard to the desired data type. Following program illustrates how to read integer and float type values.

import java.io.*;  // import the package
public class IntFloatRead {
   public static void main(String args[])throws IOException {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      System.out.print("Enter Your Roll No. (integer) : ");
      String str = br.readLine(); // take a string as input from keyboard
      int roll = Integer.parseInt(str); // convert string to integer
      System.out.print("Enter Your Marks (float) : ");
      str = br.readLine();
      float marks = Float.parseFloat(str); // convert string to float
      System.out.println("Roll = " + roll);
      System.out.println("Marks = " + marks);
   }
}
Run this program in your system to take input at run-time  


Console class
Console class was included in Java 6. Following program illustrates the use of Console class to perform I/O.

import java.io.*;  // import the package
public class ConsoleDemo {
   public static void main(String args[]) {
      Console cio = System.console();
      System.out.println("Enter Your name");
      String name = cio.readLine();
      System.out.println("Hello " + name);
   }
}
Run this program in your system to take input at run-time  


Scanner class
Scanner class is a part of java.util package and it was introduced in Java 5. Following program demonstrates the use of Scanner class to perform I/O.

import java.util.Scanner;  // import the Scanner class from util package
public class ScannerDemo {
   public static void main(String args[]) {
      Scanner sio = new Scanner(System.in);
      System.out.println("Enter a string");
      String str = sio.nextLine(); // read a line
      System.out.println("You have entered " + str);
      System.out.print("Enter an Integer Value : ");
      int val1 = sio.nextInt(); // read an integer
      System.out.print("Enter an Float Value : ");
      float val2 = sio.nextFloat(); // read a float
      System.out.println("Integer : " + val1);
      System.out.println("Float : " + val2);
      /* 'double' can be read using sio.nextDouble() and 'long' value can
         be read using sio.nextFloat().
         Similarly, nextByte() and nextShort are also available.
         sio.next() would read a string but stops reading any more character
         when it sees a space character. */
   }
}
Run this program in your system to take input at run-time  


DataInputStream class
Following program demonstrates the use of DataInputStream class to perform I/O.

import java.io.*;  // import the package
public class DISdemo {
   public static void main(String args[]) throws IOException {
      DataInputStream dis = new DataInputStream(System.in);
      System.out.println("Enter your name : ");
      String name = dis.readLine();
      System.out.println("Hello " + name);
   }
}
Run this program in your system to take input at run-time  

I/O using DataInputStream class is deprecated and it is recommended not to use this mechanism. Use any of the three techniques described above.

Back | Next