OutputStream Class in Java

Last Updated : 20 Nov, 2026

The OutputStream class in Java is an abstract class in the java.io package used to write bytes to a destination such as a file or network connection. It is the superclass of all byte output stream classes.

  • Provides methods to write individual bytes or byte arrays.
  • Subclasses must implement the write(int b) method to define how bytes are written.

Constructor of OutputStream Class

The OutputStream class provides a no-argument constructor.

OutputStream()

It is called when a subclass creates an output stream object. Since OutputStream is abstract, it cannot be instantiated directly.

Methods of OutputStream Class

The following methods are available in the OutputStream class.

Method

Description

close()

Closes the stream and releases associated resources.

flush()

Flushes buffered output bytes, if supported by the stream.

write(int b)

Writes one byte to the output stream.

write(byte[] b)

Writes all bytes from the specified array.

write(byte[] b, int off, int len)

Writes len bytes starting at index off.

nullOutputStream()

Returns an output stream that discards all written bytes. Available since Java 11.

Syntax

public void close() throws IOException
public void flush() throws IOException
public abstract void write(int b) throws IOException
public void write(byte[] b) throws IOException
public void write(byte[] b, int off, int len) throws IOException
public static OutputStream nullOutputStream()

Here, b represents a byte value or array, off specifies the starting index, and len indicates the number of bytes to write.

Note: The base implementation of flush() performs no action. The nullOutputStream() method is static and discards written bytes.

Example: Program demonstrates how to write bytes to a file using FileOutputStream, a subclass of OutputStream.

Java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

class OutputStreamDemo {
    public static void main(String[] args)
        throws IOException
    {

        // Automatically closes the stream
        try (OutputStream os
             = new FileOutputStream("file.txt")) {

            byte[] b = { 65, 66, 67, 68, 69, 70 };

            // Write the byte array
            os.write(b);

            // Flush buffered data if applicable
            os.flush();

            // Write individual bytes
            for (int i = 71; i < 75; i++) {
                os.write(i);
            }
        }

        System.out.println("Data written successfully.");
    }
}

Output:

Data written successfully.

Screenshot-2026-11-20-121550

Explanation:

  • FileOutputStream creates file.txt and writes bytes to it.
  • write(byte[] b) writes byte values 65–70, representing characters A–F in ASCII.
  • flush() requests that buffered output be forwarded, if applicable.
  • write(int b) writes byte values 71–74, representing G–J.
  • Try-with-resources automatically closes the stream, even if an exception occurs during writing.
Comment