Fundamentals 8 min read

Understanding the Differences Between Java Byte Streams and Character Streams

This article explains how Java byte streams operate directly on files without buffering while character streams use an intermediate buffer, demonstrates the effects with code examples, and discusses when to use each stream type in practice.

Java Captain
Java Captain
Java Captain
Understanding the Differences Between Java Byte Streams and Character Streams

Byte streams and character streams in Java are used similarly, but they differ in how they handle data: byte streams write directly to the file without a buffer, whereas character streams write to an internal memory buffer before flushing to the file.

Example: Byte stream without closing the stream

package org.lxh.demo12.byteiodemo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class OutputStreamDemo05 {
    public static void main(String[] args) throws Exception {
        // Find the file
        File f = new File("d:" + File.separator + "test.txt");
        OutputStream out = null; // prepare output object
        out = new FileOutputStream(f); // instantiate via polymorphism
        String str = "Hello World!!!"; // string to write
        byte b[] = str.getBytes(); // convert to byte array
        out.write(b); // write content
        // out.close(); // not closed intentionally
    }
}

The program writes "Hello World!!!" to test.txt even though the stream is not closed, showing that the byte stream writes directly to the file.

Example: Character stream without closing the stream

package org.lxh.demo12.chariodemo;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
public class WriterDemo03 {
    public static void main(String[] args) throws Exception {
        File f = new File("d:" + File.separator + "test.txt");
        Writer out = null; // prepare output object
        out = new FileWriter(f); // instantiate via polymorphism
        String str = "Hello World!!!"; // string to write
        out.write(str); // write content
        // out.close(); // not closed intentionally
    }
}

Running this code produces an empty file because the character stream’s buffer is never flushed; without closing (or flushing) the buffered data is not written to disk.

To force the buffered data to be written without closing, the flush() method can be used:

package org.lxh.demo12.chariodemo;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
public class WriterDemo04 {
    public static void main(String[] args) throws Exception {
        File f = new File("d:" + File.separator + "test.txt");
        Writer out = new FileWriter(f);
        String str = "Hello World!!!";
        out.write(str);
        out.flush(); // force buffer to be written
        // out.close(); // still not closed
    }
}

After flushing, the file contains the expected content, confirming that character streams rely on a memory buffer.

A buffer is simply a region of memory used to temporarily store data to improve performance when repeatedly accessing a resource such as a file or database. In character streams, all characters are first held in this buffer before being written.

When deciding between byte streams and character streams, byte streams are generally preferred for raw binary data (including files, images, etc.) because they operate directly on bytes, while character streams are suited for text data that requires encoding handling.

Java stream hierarchy summary:

Byte streams: InputStream (ancestor of all byte input streams) and OutputStream (ancestor of all byte output streams).

Character streams: Reader (ancestor of all character input streams) and Writer (ancestor of all character output streams).

Both InputStream , OutputStream , Reader , and Writer are abstract classes and cannot be instantiated directly.

Conversion between byte and character streams is performed with InputStreamReader and OutputStreamWriter , which handle charset encoding and decoding.

JavaI/OBufferfile handlingByte StreamCharacter Stream
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.