Skip to main content

Output Stream Writer Example in Java

2 min read Updated May 29, 2026
Share:
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

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.io.*; imports a class used later in the program.

  3. OutputStream out = new FileOutputStream("newfile.txt"); updates a variable used in the calculation or output.

  4. OutputStreamWriter outw = new OutputStreamWriter(out); updates a variable used in the calculation or output.

  5. FileInputStream ins = new FileInputStream("newfile.txt"); updates a variable used in the calculation or output.

  6. A println / print call writes text to the console — part of the sample output below.

  7. A println / print call writes text to the console — part of the sample output below.

  8. A println / print call 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 .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Output Stream Writer program demonstrate?
It shows how to implement output stream writer in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials