Video in progress
What is the difference between a byte stream and a character stream in Java?
A byte stream (InputStream/OutputStream) works with raw bytes: images, zip files, any binary file. A character stream (Reader/Writer) turns bytes into characters through a charset and is meant for text.
📖 Answer
On disk everything is bytes. The difference is who turns them into letters.
// binary file: bytes stay untouched
try (InputStream in = new FileInputStream("logo.png")) {
byte[] data = in.readAllBytes();
}
// text: bytes are turned into chars through UTF-8
try (Reader r = new InputStreamReader(new FileInputStream("notes.txt"), StandardCharsets.UTF_8)) {
int ch = r.read(); // one character, not one byte
}
The rule is simple: for text use Reader/Writer, for everything else use streams. If you read an image with a Reader, the charset decoder "corrupts" the bytes and the file cannot be restored.
InputStreamReader is the bridge between the two worlds: it takes a byte stream and gives you a character stream based on a charset.
Sign up to mark studied questions and save your progress.