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:
|
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
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. |
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. |
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 = 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
No comments:
Post a Comment