Home Java Example Java Io Read Text from Standard IO
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Read Text from Standard IO

Advertisement
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 System.in 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.
System.in is a byte stream that has no character stream features. To use Standard Input as a character stream, wrap System.in within the InputStreamReader as an argument.

InputStreamReader inp = new InputStreamReader(system.in);

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 java.io 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 java.io.Reader class.

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

InputStreamReader:

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(system.in)

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. 
BufferedReader
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..

import java.io.*;

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

  InputStreamReader inp = new InputStreamReader(System.in)
    BufferedReader br = new BufferedReader(inp);

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


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

    System.out.println(str);
  }
}

 

Output of the Program:

C:\nisha>javac ReadStandardIO.java

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

C:\nisha>

Download this example

Advertisement

Liked it!  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.

Posted on: April 16, 2007

Ask Questions?    Discuss: Read Text from Standard IO   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Srun Kagna
May 31, 2011
Your mistake code...

Hello Sir; I just look your example's code. In this sentence's : String str = in.readLine(); not correct. Pls, in to br This full sentence is : String str = br.readLine(); Thank you... :)
anonymous
June 29, 2011
Typo in program code

in.readLine(); // Error , replace by the following br.readLine();
Tenzin Choeku
October 18, 2011
program code above! and i had made some changes

import java.io.*; public class ReadStandardIO{ public static void main(String[] args) throws IOException{ InputStreamReader inp = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(inp); System.out.println("Enter text : "); String str = br.readLine(); System.out.println("You entered String : "); System.out.println(str); } }
JavaCoder
November 16, 2011
JAVA

insted of this line > String str = in.readLine(); it should be > String str = br.readLine();
Arun.
December 13, 2011
Bug in Read Text from Standard IO Example.

String str = in.readLine(); is wrong. Change it to String str = br.readLine();
Sahi
March 12, 2013
typo

String str = in.readLine(); => String str = br.readLine();
Sarabjeet
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 - Ccipher.java:53: cannot find symbol symbol : variable Char location: class Ccipher pt[i]=Char.parseChar(stdin.readLine()); ^ Ccipher.java:98: cannot find symbol symbol : variable Char location: class Ccipher pt[i]=Char.parseChar(stdin.readLine()); ^ 2 errors
Gobinathan
February 3, 2012
Java

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(System.in) 2. br.readLine() insted of in.readline() String str = in.readLine();
amr
April 5, 2012
feedback relating to typing error in program.....

semi colon missing:InputStreamReader inp = new InputStreamReader(System.in) 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?
Pratik
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();
ritika
July 19, 2012
question:

1.how do we read a single character from keyboard??
Mahmoud
August 26, 2012
CnrTEuUumCkxV

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
java

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