Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the difference between InputStream/OutputStream and Reader/Writer? How does InputStreamReader connect them, and what goes wrong if you do not pass a charset?

InputStream and OutputStream move raw bytes, while Reader and Writer move chars, which are text. InputStreamReader is the bridge that decodes bytes into chars using a Charset, and OutputStreamWriter encodes chars back into bytes. If we do not pass a charset, the JVM's default is used: since Java 18 that is UTF-8 everywhere (JEP 400), but before that it depended on the operating system's locale, so the same code could produce different text on different machines.

Why this question is asked

Every file, socket and HTTP body is a sequence of bytes, yet most of our code works with Strings. The step between the two, decoding and encoding, is where garbled names, question marks and broken accents come from. Interviewers ask this question because it shows whether we know where that step happens in Java and what decides how it is done. In this answer, we will recap bytes, chars and charsets, compare the two families of streams, see how InputStreamReader and OutputStreamWriter connect them, and then look at what happens without an explicit charset before and after Java 18.

1A Quick Recap: Bytes, Chars and Charsets

A byte is 8 bits, a number from 0 to 255 (Java's byte type stores it as -128 to 127). Files and networks only ever carry bytes. A char in Java is a 16-bit UTF-16 code unit, and a String is a sequence of chars. Text is something we read; bytes are how text is stored.

A charset, or character encoding, is the rule that maps text to bytes and back. UTF-8 stores the letter A as one byte, 0x41, and the letter e with an acute accent as two bytes, 0xC3 0xA9. Windows-1252, an old Western European code page, stores the same accented letter as the single byte 0xE9. If one side writes with UTF-8 and the other reads with Windows-1252, the reader sees two wrong characters instead of one right one. That mismatch is the root of nearly every garbled-text bug.

2Two Families of Streams

Java's java.io package has two parallel families. InputStream and OutputStream, with subclasses such as FileInputStream and ByteArrayOutputStream, read and write bytes. Their read() method returns one byte as an int from 0 to 255, or -1 at the end. They know nothing about text, which is exactly right for images, ZIP files, encrypted data or any binary format.

Reader and Writer, with subclasses such as BufferedReader and StringWriter, read and write chars. Their read() returns one char as an int, and BufferedReader adds readLine(). They are what we want for text. But since storage holds bytes, a Reader that reads a file must decode those bytes somewhere, and that requires a charset.

Byte streams and character streams
Byte streamsCharacter streams
Base classesInputStream, OutputStreamReader, Writer
Unit of databyte (8 bits)char (16-bit UTF-16 unit)
Typical useimages, archives, any binary datatext files, CSV, JSON as text
Needs a charset?noyes, to convert to and from bytes
BridgeInputStreamReader / OutputStreamWriterwraps a byte stream

3How InputStreamReader and OutputStreamWriter Connect Them

InputStreamReader wraps an InputStream and is itself a Reader. When we ask it for chars, it reads bytes from the stream underneath and decodes them with its Charset. OutputStreamWriter does the reverse: we give it chars, it encodes them with its Charset and writes bytes to the OutputStream underneath. We usually add a buffer on top for speed and for readLine().

Java
byte[] bytes = "caf\u00e9".getBytes(StandardCharsets.UTF_8);
System.out.println(bytes.length); // 5 (c a f, then 2 bytes for the accented e)
try (Reader r = new InputStreamReader(new ByteArrayInputStream(bytes), StandardCharsets.UTF_8)) {
System.out.println(new BufferedReader(r).readLine()); // cafe with the accent, correct
}
try (Reader r = new InputStreamReader(new ByteArrayInputStream(bytes), Charset.forName("windows-1252"))) {
System.out.println(new BufferedReader(r).readLine()); // caf plus two odd characters (mojibake)
}
try (Writer w = new OutputStreamWriter(new FileOutputStream("out.txt"), StandardCharsets.UTF_8)) {
w.write("caf\u00e9"); // out.txt holds 5 bytes
}

Notice that the bytes are the same in both reads. Only the charset given to the bridge differs, and that alone decides whether we get the right text. The wrong-looking output has a name, mojibake, and it always means bytes were decoded with a different charset than the one used to encode them.

4What Happens Without a Charset

Many convenient constructors and methods take no charset: new InputStreamReader(in), new FileReader(file), new FileWriter(file), new String(bytes) and text.getBytes(). They all use the JVM's default charset, which we can see with Charset.defaultCharset().

Before Java 18, the default charset came from the operating system's locale settings when the JVM started. On most Linux servers and on macOS it was UTF-8, but on Windows it was usually a legacy code page such as Windows-1252, and a Linux container with a minimal locale could default to US-ASCII. So the same jar wrote different bytes on different machines, and a file written on one could be misread on another.

Java 18 changed this with JEP 400: the default charset is UTF-8 on every platform, regardless of locale. That made defaults predictable, but it also means code that silently relied on a Windows code page changes behaviour when upgraded. The system property file.encoding can be set to COMPAT to bring back the old locale-based default during a migration.

Java
System.out.println(Charset.defaultCharset()); // Java 21 on any OS: UTF-8
// Java 11 on Windows: windows-1252 (typically)
// Java 11 on Linux/macOS: UTF-8 (typically)

The safe habit is therefore to pass StandardCharsets.UTF_8 explicitly whenever we turn bytes into text or text into bytes for files and network data. The code then says what the data format is, behaves the same on every JVM version and platform, and passes static checks that flag default-charset calls. Newer APIs make this easy: Files.readString(path, UTF_8), Files.newBufferedReader(path, UTF_8), and FileReader and FileWriter constructors that take a Charset since Java 11.

5Follow-up questions

What was file.encoding used for before Java 18?

Before Java 18, the file.encoding system property held the name of the default charset, which the JVM derived from the operating system locale at startup. Setting -Dfile.encoding=UTF-8 on the command line was the common way to force a UTF-8 default on Windows, although officially the property was read-only and setting it was not fully supported in every case. Since Java 18 the default is UTF-8 and the only supported values are UTF-8 and COMPAT, where COMPAT restores the old locale-based behaviour. The locale-derived encoding is still available as the native.encoding property, for code that genuinely needs to match the operating system, such as text exchanged with legacy local tools.

Why not just use byte streams for text and call new String at the end?

We can, if we read all the bytes first and then decode with an explicit charset, as Files.readString does. What goes wrong is decoding chunk by chunk: a multi-byte character can be split across two chunks, and decoding each chunk separately corrupts it. A Reader keeps the decoder's state between reads, so it handles split characters correctly.

Does System.out use the same default charset?

Not necessarily. Since Java 18, System.out and System.err use stdout.encoding and stderr.encoding, which follow the console's encoding rather than file.encoding, so printing to a Windows console can still use a console code page even though files default to UTF-8.

6Conclusion

In this answer, we saw that byte streams carry raw bytes and character streams carry chars, and that InputStreamReader and OutputStreamWriter are the bridge that decodes and encodes with a Charset. When no charset is given, the default is used, which was locale-dependent before Java 18 and is UTF-8 everywhere since JEP 400. Passing StandardCharsets.UTF_8 explicitly keeps text handling the same on every platform and every JVM version.

What to say in the interviewAbout 80 seconds aloud

Open with

InputStream and OutputStream work with raw bytes, which is right for binary data like images or archives. Reader and Writer work with chars, so they are for text.

Then, if they want more

Since files and sockets only hold bytes, something has to convert, and that is InputStreamReader and OutputStreamWriter: InputStreamReader wraps an InputStream and decodes the bytes into chars with a Charset, and OutputStreamWriter encodes chars into bytes. The charset is the whole story.

The same bytes decoded with UTF-8 or with Windows-1252 give different text, and that mismatch is where mojibake comes from. If I do not pass a charset, for example new FileReader(file), new String(bytes) or getBytes(), Java uses the default charset.

Before Java 18 the default came from the operating system locale, so it was usually UTF-8 on Linux and Mac but a legacy code page on Windows, and code that worked on a laptop could garble text on a Windows server.

JEP 400 in Java 18 made UTF-8 the default everywhere, which is more predictable but can change behaviour on upgrade, and file.encoding=COMPAT brings the old behaviour back for migration. My rule is to always pass StandardCharsets.UTF_8 explicitly for files and network data, so the code states its format and does not depend on where or on which version it runs.

About this answer

Last updated
12 November 2026
Versions it names
Java 18, Java 11, Java 21
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between InputStream/OutputStream and Reader/Writer? How does...": https://scrivio-interview-library.netlify.app/java/io-and-nio/44ffd6eb8b/