Home » Input from console in Java

Input from console in Java

by Online Tutorials Library

Reading data from keyboard

There are many ways to read data from the keyboard. For example:

  • InputStreamReader
  • Console
  • Scanner
  • DataInputStream etc.

InputStreamReader class

InputStreamReader class can be used to read data from keyboard.It performs two tasks:

  • connects to input stream of keyboard
  • converts the byte-oriented stream into character-oriented stream

BufferedReader class

BufferedReader class can be used to read data line by line by readLine() method.

Example of reading data from keyboard by InputStreamReader and BufferdReader class

In this example, we are connecting the BufferedReader stream with the InputStreamReader stream for reading the line by line data from the keyboard.

Output:Enter your name        Amit        Welcome Amit 

keyboard


Another Example of reading data from keyboard by InputStreamReader and BufferdReader class until the user writes stop

In this example, we are reading and printing the data until the user prints stop.

Output:Enter data: Amit        data is: Amit        Enter data: 10        data is: 10        Enter data: stop        data is: stop 
Next TopicConsole class

You may also like