InputStreamReader Class in Java: From Bytes to Characters
Networks, files and the keyboard deliver bytes, but programs work with text. InputStreamReader is the bridge between the two: it reads bytes from any InputStream and decodes them into characters using a charset.
This tutorial covers its constructors and methods, and two examples: decoding text received over a connection, and reading a file created with an older Windows encoding.
1. What Is InputStreamReader in Java?
InputStreamReader extends Reader and wraps an InputStream. Each read may pull one or more bytes from the stream and turn them into characters with the chosen charset. It was added in Java 1.1, and it is the parent class of FileReader.
2. InputStreamReader Class Hierarchy
Swipe sideways to see the whole diagram.
3. Constructors of InputStreamReader
| Constructor | Description |
|---|---|
InputStreamReader(InputStream in, Charset cs) | Decodes with the given charset (Java 1.4). The recommended form. |
InputStreamReader(InputStream in, String charsetName) | Charset by name; throws UnsupportedEncodingException for unknown names. |
InputStreamReader(InputStream in, CharsetDecoder dec) | Uses a decoder you configure yourself (Java 1.4). |
InputStreamReader(InputStream in) | Uses the default charset (UTF-8 since Java 18). |
4. Commonly Used Methods
| Method | Description |
|---|---|
int read() | Next decoded character, or -1 at the end. |
int read(char[] cbuf, int off, int len) | Decodes up to len characters into the array. |
boolean ready() | Whether a read will not block. |
String getEncoding() | The charset name in use; historical names such as UTF8 may be returned. |
void close() | Closes the reader and the wrapped stream. |
5. Example: Decoding Text Received as Bytes
A weather client receives a UTF-8 response as bytes. InputStreamReader decodes them, and a BufferedReader splits the text into lines.
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class NetworkTextReader {
public static void main(String[] args) throws IOException {
// Pretend these bytes arrived from a web server, encoded as UTF-8
byte[] response = "Status: OK\nCity: München\nTemp: 18°C\n".getBytes(StandardCharsets.UTF_8);
InputStream network = new ByteArrayInputStream(response);
try (InputStreamReader decoder = new InputStreamReader(network, StandardCharsets.UTF_8);
BufferedReader lines = new BufferedReader(decoder)) {
System.out.println("Decoding with: " + decoder.getEncoding());
int chars = 0, count = 0;
String line;
while ((line = lines.readLine()) != null) {
chars += line.length();
count++;
}
System.out.println("Bytes received: " + response.length);
System.out.println("Lines decoded: " + count);
System.out.println("Characters found: " + chars + " (without line breaks)");
}
}
}
Decoding with: UTF8 Bytes received: 38 Lines decoded: 3 Characters found: 33 (without line breaks)
How the code works
- A
ByteArrayInputStreamstands in for the network connection. new InputStreamReader(network, StandardCharsets.UTF_8)decodes the bytes;getEncoding()reports the historical nameUTF8.- The 38 bytes become 33 characters plus line breaks, because
üand°each take two bytes in UTF-8.
6. Example: Reading a Legacy Windows File
An old Windows accounting tool exported coupon text in the windows-1252 charset, where the euro sign is a single byte. Reading it with the right charset matters.
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class LegacyFileReader {
static String decode(byte[] bytes, Charset charset) throws IOException {
StringBuilder text = new StringBuilder();
try (Reader in = new InputStreamReader(new ByteArrayInputStream(bytes), charset)) {
int c;
while ((c = in.read()) != -1) {
text.append((char) c);
}
}
return text.toString();
}
public static void main(String[] args) throws IOException {
Charset windows = Charset.forName("windows-1252");
String expected = "€5 off"; // "EUR 5 off" with the euro sign
byte[] legacyFile = expected.getBytes(windows); // an old export from a Windows tool
System.out.println("Bytes in file: " + legacyFile.length);
System.out.println("Read as windows-1252 correct: " + decode(legacyFile, windows).equals(expected));
System.out.println("Read as UTF-8 correct: " + decode(legacyFile, StandardCharsets.UTF_8).equals(expected));
}
}
Bytes in file: 6 Read as windows-1252 correct: true Read as UTF-8 correct: false
How the code works
Charset.forName("windows-1252")looks up the legacy charset, in which the six characters take six bytes.- Decoding with the same charset restores the euro sign exactly.
- Decoding the same bytes as UTF-8 fails to rebuild the euro sign, so the text is wrong.
7. Common Mistakes and Best Practices
- Omitting the charset. On Java 17 and earlier the default depended on the operating system; always pass one.
- Reading line by line without
BufferedReader.InputStreamReaderhas noreadLine(). - Closing
System.in. Closing a reader that wraps it closes standard input for the rest of the program. - Guessing charsets. Use the charset the data was written with; for new data, use UTF-8.
8. Java Version Improvements and Java 25 Updates
| Java version | Change |
|---|---|
| Java 1.1 | InputStreamReader introduced. |
| Java 1.4 | Constructors that take a Charset or CharsetDecoder. |
| Java 18 | The default charset is UTF-8 on every platform (JEP 400). |
| Java 25 | No API changes. For keyboard input, java.lang.IO.readln() is a simpler option. |
9. Summary
InputStreamReaderturns bytes from anyInputStreaminto characters.- Always pass the charset the data was written with.
- Wrap it in a
BufferedReaderto read whole lines efficiently. - It is the parent class of
FileReader.
Related Tutorials
- OutputStreamWriter Class in Java
- BufferedReader Class in Java
- FileReader Class in Java
- Byte Stream vs Character Stream