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:
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
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.
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)
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
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, 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.
|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..
Output of the Program:
Enter text :
this is an Input Stream
You entered String :
this is an Input Stream
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial