Output Stream Writer Example in Java
On this page (10sections)
Introduction
Output Stream Writer is a classic Java console program that demonstrates the concept with complete source code and sample output. Java I/O reads and writes bytes and characters from files, streams and the console.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Definition
The OutputStream class is an abstract superclass that provides a minimal programming interface and a partial implementation of output streams. OutputStream defines methods for writing bytes or arrays of bytes to the stream. An output stream is automatically opened when you create it. You can explicitly close an output stream with the close method, or let it be closed implicitly when the OutputStream is garbage collected, which occurs when the object is no longer referenced. the OutputStreamWriter is used along with OutputStream.
Syntax
OutputStream Variable_name1 = new FileOutputStream(File_location);
OutputStreamWriter Variable_name2 = new OutputStreamWriter(Variable_name1);
Output Stream Writer Example Program
import java.io.*;
public class OutputStreamWriterDemo {
public static void main(String[] args) {
try {
OutputStream out = new FileOutputStream("newfile.txt");
OutputStreamWriter outw = new OutputStreamWriter(out);
FileInputStream ins = new FileInputStream("newfile.txt");
outw.write(70);
outw.flush();
System.out.println("" + (char) ins.read());
System.out.println("Stream is being closed");
outw.close();
System.out.println("Stream closed successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sample Output
F
Stream is being closed
Stream closed successfully.
When to use
Use this output stream writer example when learning or revising core Java syntax.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
import java.io.*;imports a class used later in the program. -
OutputStream out = new FileOutputStream("newfile.txt");updates a variable used in the calculation or output. -
OutputStreamWriter outw = new OutputStreamWriter(out);updates a variable used in the calculation or output. -
FileInputStream ins = new FileInputStream("newfile.txt");updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below.
Best Practices
- Use meaningful variable and class names that describe their purpose.
- Compile and run the program locally — modify values to see how output changes.
- Read compiler errors carefully; they usually point to the exact line to fix.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.