Backend Development 7 min read

Understanding Java I/O: Streams, Decorators, and Practical Examples

This article explains Java's I/O system, detailing how streams and decorator patterns enable flexible file reading and writing, and provides clear code examples illustrating BufferedReader, FileReader, InputStream, and OutputStream usage.

Java Captain
Java Captain
Java Captain
Understanding Java I/O: Streams, Decorators, and Practical Examples

The most important function of a computer is data processing, and a useful programming language must provide robust I/O capabilities to ingest raw data and output processed results.

Compared with many languages where I/O operations like file reading are encapsulated in one or two lines, Java's I/O appears complex because developers often need multiple layers of decorations (decorator pattern) to achieve the same tasks.

This complexity brings flexibility: Java programmers can control the entire I/O flow and design optimal I/O strategies.

IO Example

Assume a file Hello World! Hello Nerd! named file.txt .

File‑reading example:

import java.io.*;
public class Test {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
br.close();
} catch (IOException e) {
System.out.println("IO Problem");
}
}
}

The program uses a try...catch block for exception handling (see linked Java exception tutorial).

Decorator and Function Composition

The key to I/O is creating a BufferedReader br = new BufferedReader(new FileReader("file.txt")); object.

During creation, a FileReader reads byte streams from the file and converts them to a Unicode text stream. BufferedReader decorates this FileReader , adding buffered reading and line‑by‑line access via readLine() .

BufferedReader acts as a decorator: it receives an original object and returns a more feature‑rich object, allowing the same decoration to be applied to other streams such as standard input or network streams.

The data flow diagram (shown in the original article) illustrates how data moves upward through these layers, similar to Linux text‑stream concepts.

More Combinations

Java offers many decorators. For finer control, one can combine FileInputStream + InputStreamReader instead of FileReader , separating byte reading from character conversion.

The four fundamental I/O base classes are InputStream , OutputStream , Reader , and Writer , all residing in the java.io package. Their inheritance hierarchy is illustrated in the article.

IOException has several derived classes, also shown in a diagram.

Reader and Writer handle Unicode text (e.g., BufferedReader , InputStreamReader , FileReader ), while InputStream and OutputStream handle raw byte streams, useful for reading compressed files or binary data.

Writing

Writing follows a similar decorator pattern. Example of writing text to a file:

import java.io.*;
public class Test {
public static void main(String[] args) {
try {
String content = "Thank you for your fish.";
File file = new File("new.txt");
// create the file if it doesn't exist
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
System.out.println("IO Problem");
}
}
}

This code creates a File object, then uses FileWriter and BufferedWriter (decorators) to write the string to new.txt .

Summary

The article provides a basic introduction to Java I/O, highlighting its relative complexity, the decorator pattern that offers flexibility, and the essential classes for handling both byte and character streams.

javaStreamsIOdecoratorFileReaderBufferedReader
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.