Unit 2 I_O Streams.pptx
Unit 2 I_O Streams.pptx
I/O Streams
Introduction
In Java, streams are the sequence of data that are read from the source and written to the
destination.
An input stream is used to read data from the source. And, an output stream is used to write data to
the destination.
in our first Hello World example, we have used System.out to print a string.
● Byte Stream- Byte stream is used to read and write a single byte (8 bits) of
data. All byte stream classes are derived from base abstract classes called
InputStream and OutputStream.
● Character Stream- Character stream is used to read and write a single
character of data. All the character stream classes are derived from base
abstract classes Reader and Writer.
Java InputStreamReader Class
The InputStreamReader class of the java.io package can be used to convert data in bytes
into data in characters.
The InputStreamReader class works with other input streams. It is also known as a bridge
between byte streams and character streams. This is because the InputStreamReader
reads bytes from the input stream as characters.
For example, some characters required 2 bytes to be stored in the storage. To read such
data we can use the input stream reader that reads the 2 bytes together and converts
into the corresponding character.
Java BufferedReader
The BufferedReader class of the java.io package can be used with other readers to read
data (in characters) more efficiently.
It extends the abstract class Reader.
The BufferedReader maintains an internal buffer of 8192 characters.
During the read operation in BufferedReader , a chunk of characters is read from the disk
and stored in the internal buffer. And from the internal buffer characters are read
individually.
Hence, the number of communication to the disk is reduced. This is why reading
characters is faster using BufferedReader.
Reading data from console by InputStreamReader and BufferedReader
import java.io.*;
String name=br.readLine();
System.out.println("Welcome "+name);
} }