Share on Google+Share on Google+

Read Text from Standard IO

In this section, you will see how the standard I/O is used to input any thing by the keyboard or a file.

Reading Text from the Standard Input


Standard Streams:

Standard Streams are a feature provided by many operating systems. By default, they read input from the keyboard and write output to the display. They also support I/O operations on files.

 Java also supports three Standard Streams:

  • Standard Input: Accessed through which is used to read input from the keyboard.
  • Standard Output: Accessed through System.out  which is used to write output to be display.
  • Standard Error: Accessed through System.err which is used to write error output to be display.

These objects are defined automatically and do not need to be opened explicitly. Standard Output and Standard Error, both are to write output; having error output separately so that the user may read error messages efficiently. is a byte stream that has no character stream features. To use Standard Input as a character stream, wrap within the InputStreamReader as an argument.

InputStreamReader inp = new InputStreamReader(;

Working with Reader classes:

Java provides the standard I/O facilities for reading text from either the file or the keyboard on the command line. The Reader class is used for this purpose that is available in the package. It acts as an abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). the Reader class is further categorized into the subclasses. 

The following diagram shows a class-hierarchy of the class.

However, most subclasses override some of the methods in order to provide higher efficiency, additional functionality, or both.


An InputStreamReader is a bridge from byte streams to character streams i.e. it reads bytes and decodes them into Unicode characters according to a particular platform. Thus, this class reads characters from a byte input stream. When you create an InputStreamReader, you specify an InputStream from which, the InputStreamReader reads the bytes.

The syntax of InputStreamReader is written as:

InputStreamReader <variable_name> = new InputStreamReader(

BufferedReader : 

The BufferedReader class is the subclass of the Reader class. It reads character-input stream data from a memory area known as a buffer maintains state.  The buffer size may be specified, or the default size may be used that is large enough for text reading purposes. 
converts an unbuffered stream into a buffered stream using the wrapping expression, where the unbuffered stream object is passed to the constructor for a buffered stream class.

For example the constructors of the BufferedReader class shown as:

BufferedReader(Reader in):Creates a buffering character-input stream that uses a default-sized input buffer.

BufferedReader(Reader in, int sz): Creates a buffering character-input stream that uses an input buffer of the specified size.

BufferedReader class provides some standard methods to perform specific reading operations shown in the table. All methods throws an  IOException, if an I/O error occurs.

 Method  Return Type  Description
 read( )  int  Reads a single character 
 read(char[] cbuf, int off, int len)  int  Read characters into a portion of an array.
 readLine( )  String  Read a line of text. A line is considered to be  terminated by ('\n').
  close( )  void   Closes the opened stream.

  This program illustrates you how to use standard input stream to read the user input..


public class ReadStandardIO{
public static void main(String[] argsthrows IOException{

  InputStreamReader inp = new InputStreamReader(
    BufferedReader br = new BufferedReader(inp);

    System.out.println("Enter text : ");
String str = in.readLine();

   System.out.println("You entered String : ");



Output of the Program:


C:\nisha>java ReadStandardIO
Enter text :
this is an Input Stream
You entered String :
this is an Input Stream


Download this example



Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Read Text from Standard IO   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
January 10, 2013
having problem in geting character input from user

Hi there...pls help me as i m geting an error reading CHARACTER input from user...i m eting as problem in parsing d program !! pls reply asap !! Getting these 2 errors - cannot find symbol symbol : variable Char location: class Ccipher pt[i]=Char.parseChar(stdin.readLine()); ^ cannot find symbol symbol : variable Char location: class Ccipher pt[i]=Char.parseChar(stdin.readLine()); ^ 2 errors
February 3, 2012

Give me all possible input statements with example....
Rajesh Kumar Patel
February 7, 2012
B.E.- Java Prof Working

Their is Two error 1. Semicolon at the end of InputStreamReader inp = new InputStreamReader( 2. br.readLine() insted of in.readline() String str = in.readLine();
April 5, 2012
feedback relating to typing error in program.....

semi colon missing:InputStreamReader inp = new InputStreamReader( in is not valid object:String str = in.readLine();
Sivan Mehta
May 20, 2012
This post

The sample code you provided doesn't even compile. How is supposed to be used?
July 2, 2012
In the example one change is required

In the example above instead of in.readLine(), it should be br.readLine(). String str = in.readLine(); should be replaced with String str = br.readLine();
July 19, 2012
question: do we read a single character from keyboard??
August 26, 2012

Hi Alexander,Nice trick invoking a sicrpt :)I think the problem is in this line of your ProcessBuilder params:> commands.add( \ image Over 0,0 0,0 im-watermark.png'\ ");If you re-write that as:> commands.add( image );> commands.add( Over );> commands.add( 0,0 );> commands.add( 0,0 );> commands.add( im-watermark.png ); it should work.
poonam kumawat
May 24, 2014

scanf("%d",&mid1); what is the code in java for this line.