Reading Console Input in Java

You are currently viewing Reading Console Input in Java

Reading console input in simple java console applications is a very straight forward process. Java provides developers two predefined streams in the java.lang.System class to read and write data to and from standard input and output devices. System.out is a standard output stream which is Console by default and System.in is predefined input stream which is Keyboard by default. To read user input in a console window we connect system.in with other stream classes available in java.io package.

Following code example shows you how to read user input in console window in java.

import java.io.*;

public class ConsoleInput
{
      public static void main(String[] args) throws Exception
      {
            System.out.println("Enter Your Text: " );   
            InputStreamReader r = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(r); 
            String s = br.readLine(); 
            System.out.println(s);     
      }
}
READ ALSO:  Singleton Design Pattern in Java

Leave a Reply