InputStreamReader
is a bridge from byte streams to character streams. It reads bytes from an underlying InputStream
and decodes them into characters using a specified charset (encoding). This is essential for reading text data from byte-based input sources like files, network connections, or system input (keyboard).
Commonly Used Constructors and Methods

Simple Program: Reading Console Input Using InputStreamReader
Read user input from the keyboard and display it on the console.
import java.io.*; public class InputStreamReaderSimpleDemo { public static void main(String[] args) { try { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.println("Enter text (type 'exit' to quit):"); String line; while (!(line = br.readLine()).equalsIgnoreCase("exit")) { System.out.println("You typed: " + line); } br.close(); } catch (IOException e) { e.printStackTrace(); } } }
Problem Statement:
LotusJavaPrince needs to read a UTF-8 encoded text file data_utf8.txt
which contains multilingual text. Mahesh suggests using InputStreamReader
with UTF-8 charset to ensure correct decoding.
import java.io.*; public class UTF8FileReader { public static void main(String[] args) { try (InputStreamReader isr = new InputStreamReader(new FileInputStream("data_utf8.txt"), "UTF-8"); BufferedReader br = new BufferedReader(isr)) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println("Error reading file."); e.printStackTrace(); } } }
The InputStreamReader
class is a key component in Java I/O that serves as a bridge between byte streams and character streams. It enables developers to read character data from sources that originally provide data in byte format, such as files, network sockets, or standard input streams.