0% found this document useful (0 votes)
9 views8 pages

Unit 2 I_O Streams.pptx

The document provides an overview of I/O streams in Java, explaining the concepts of input and output streams, and their types: byte streams and character streams. It details the InputStreamReader class, which converts byte data to character data, and the BufferedReader class, which enhances reading efficiency by using an internal buffer. An example code snippet demonstrates how to read data from the console using InputStreamReader and BufferedReader.

Uploaded by

newmovies1638
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Unit 2 I_O Streams.pptx

The document provides an overview of I/O streams in Java, explaining the concepts of input and output streams, and their types: byte streams and character streams. It details the InputStreamReader class, which converts byte data to character data, and the BufferedReader class, which enhances reading efficiency by using an internal buffer. An example code snippet demonstrates how to read data from the console using InputStreamReader and BufferedReader.

Uploaded by

newmovies1638
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit 2

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.

Here, the System.out is a type of output stream.

Similarly, there are input streams to take input example System.in


Types of Streams

Depending upon the data a stream holds, it can be classified into:

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

It extends the abstract class Reader.

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

public class BufferedReaderExample{

public static void main(String args[]) {

InputStreamReader r=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(r);

System.out.println("Enter your name");

String name=br.readLine();

System.out.println("Welcome "+name);

} }

You might also like