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:
|
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.*;
|
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> |