00:00

Byte Stream vs Character Stream in Java

Last updated:

Java has two kinds of I/O streams, and choosing the wrong one is a classic source of bugs: garbled text, broken images and corrupted downloads. This tutorial compares byte streams and character streams side by side, with two experiments that show exactly what goes wrong when they are mixed up.

1. Byte Streams and Character Streams at a Glance

  • A byte stream moves raw 8-bit bytes and never interprets them. Its base classes are InputStream and OutputStream.
  • A character stream moves text as 16-bit char values and uses a charset to convert them to and from bytes. Its base classes are Reader and Writer.

2. How Each Stream Sees Your Data

How character streams and byte streams handle dataTop lane: a character stream turns the 4 characters C, a, f and é into 5 UTF-8 bytes, 43 61 66 C3 A9, because é needs two bytes; reading decodes them back. Bottom lane: a byte stream copies the 4 bytes 89 50 4E 47 from a PNG file unchanged.Character stream (Writer / Reader)Café436166C3A9encode with UTF-8 / decode back4 characters5 bytes: é becomes C3 A9Byte stream (InputStream / OutputStream)89504E4789504E47copied unchanged4 bytes from a PNG filethe same 4 bytes

Swipe sideways to see the whole diagram.

Underneath, every file contains bytes. A character stream adds a translation layer on top; a byte stream does not.

3. Byte Stream vs Character Stream: Comparison

AspectByte streamCharacter stream
Base classesInputStream, OutputStreamReader, Writer
Unit of data8-bit byte16-bit char
Charset handlingNone; bytes pass through unchangedEncodes and decodes using a charset
read() returns0 to 255, or -10 to 65535, or -1
Line-based readingNot availableBufferedReader.readLine()
IntroducedJava 1.0Java 1.1
Best forImages, audio, PDFs, ZIP files, exact copiesText, CSV, JSON, XML, logs

4. Example: Reading a Price Tag Both Ways

A billing screen saves a total that includes the rupee sign (₹) and then reads it back, once with a byte stream and once with a character stream.

PriceTagReader.java
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class PriceTagReader {
    public static void main(String[] args) throws IOException {
        String price = "Total: ₹499";                   // "Total: Rs 499" with the rupee sign
        Path file = Files.createTempFile("price", ".txt");
        Files.writeString(file, price);                      // saved as UTF-8

        // Byte stream: every byte becomes a char (wrong for text)
        StringBuilder viaBytes = new StringBuilder();
        try (InputStream in = Files.newInputStream(file)) {
            int b;
            while ((b = in.read()) != -1) {
                viaBytes.append((char) b);
            }
        }

        // Character stream: bytes are decoded with UTF-8
        StringBuilder viaChars = new StringBuilder();
        try (Reader in = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
            int c;
            while ((c = in.read()) != -1) {
                viaChars.append((char) c);
            }
        }

        System.out.println("Byte stream:      " + viaBytes.length() + " values, text correct: " + viaBytes.toString().equals(price));
        System.out.println("Character stream: " + viaChars.length() + " values, text correct: " + viaChars.toString().equals(price));
        Files.delete(file);
    }
}
Output
Byte stream:      13 values, text correct: false
Character stream: 11 values, text correct: true

How the code works

  1. Files.writeString() stores the 11 characters as 13 bytes, because ₹ takes three bytes in UTF-8.
  2. The byte stream turns each byte into a separate char, splitting the rupee sign into three meaningless characters.
  3. The character stream decodes the three bytes together and rebuilds the original text exactly.

5. Example: Copying a Binary File Both Ways

Now the opposite mistake: copying binary data, such as an icon, through character streams.

BinaryCopyTrap.java
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class BinaryCopyTrap {
    public static void main(String[] args) throws IOException {
        byte[] data = new byte[256];
        for (int i = 0; i < data.length; i++) {
            data[i] = (byte) i;                               // every possible byte value, like an image
        }
        Path original = Files.createTempFile("icon", ".bin");
        Files.write(original, data);

        Path byteCopy = Files.createTempFile("copy-bytes", ".bin");
        try (InputStream in = Files.newInputStream(original); OutputStream out = Files.newOutputStream(byteCopy)) {
            in.transferTo(out);
        }

        Path charCopy = Files.createTempFile("copy-chars", ".bin");
        try (Reader in = new InputStreamReader(Files.newInputStream(original), StandardCharsets.UTF_8);
             Writer out = new OutputStreamWriter(Files.newOutputStream(charCopy), StandardCharsets.UTF_8)) {
            in.transferTo(out);                               // decodes and re-encodes as if it were text
        }

        System.out.println("Original size:        " + Files.size(original) + " bytes");
        System.out.println("Byte stream copy:     " + Files.size(byteCopy) + " bytes, identical: " + (Files.mismatch(original, byteCopy) == -1));
        System.out.println("Char stream copy:     " + Files.size(charCopy) + " bytes, identical: " + (Files.mismatch(original, charCopy) == -1));
        Files.delete(original);
        Files.delete(byteCopy);
        Files.delete(charCopy);
    }
}
Output
Original size:        256 bytes
Byte stream copy:     256 bytes, identical: true
Char stream copy:     512 bytes, identical: false

How the code works

  1. The test file contains all 256 byte values, as real images do.
  2. The byte stream copy uses transferTo() and is identical to the original.
  3. The character stream decodes the bytes as UTF-8. On JDK 25, InputStreamReader replaced each of the 128 bytes that are not valid UTF-8 with the replacement character U+FFFD, which takes 3 bytes when written back, so the copy doubled to 512 bytes and the data was lost.
  4. Readers created with Files.newBufferedReader() are stricter: they throw an IOException (MalformedInputException) instead of silently replacing bytes.

6. Converting Between the Two

Sometimes you receive bytes but need text, for example from a network socket. Two bridge classes connect the families:

ClassConvertsExample
InputStreamReaderBytes to charactersnew InputStreamReader(socket.getInputStream(), UTF_8)
OutputStreamWriterCharacters to bytesnew OutputStreamWriter(out, UTF_8)

Always pass the charset explicitly so the conversion is predictable.

7. Which One Should You Use?

  • If the data is text and you need to read or change it, use a character stream with UTF-8.
  • If the data is binary, or you only need to move or copy it, use a byte stream.
  • If you only need the whole content of a small file, Files.readString() or Files.readAllBytes() is simpler.

8. Java Version Improvements and Java 25 Updates

Java versionChange
Java 1.0 and 1.1Byte streams first; character streams added in 1.1 to handle text correctly.
Java 9 and 10InputStream.transferTo() and Reader.transferTo().
Java 11Files.readString() and writeString(); charset constructors for FileReader and FileWriter.
Java 18UTF-8 is the default charset on every platform (JEP 400).
Java 25No new stream types; the comparison above applies unchanged.

9. Summary

  • Byte streams move raw bytes; character streams move text and use a charset.
  • Reading text through a byte stream breaks multi-byte characters.
  • Copying binary data through a character stream corrupts it.
  • InputStreamReader and OutputStreamWriter bridge the two families.
  • Specify UTF-8 explicitly whenever you convert between bytes and text.

Related Tutorials

10. Official Java Documentation References