Convert OutputStream to Writer in Java



Input and output Streams in Java are objects that accepts sequence of information and sends them further. These are used to read and write data from/to various sources.

What are Output Streams & Writers

A Java output streams accept output data (bytes) from a source and sends it to the destination. An output stream is represented by an abstract class known as OutputStream. This is the super class of all the OutputStream classes. There are various output streams in Java namely, ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, PipedOutputStream.

Writers are objects that are used to write data to character streams. These are represented by the abstract class Writer. This is the super class of all the writer classes. Various writers available in Java are, BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter.

Converting an OutputStream to a Writer

Converting an OutputStream to a Writer refers to wrapping a byte-based output stream with a character-based writer. This article will discuss the OutputStream, Writer, and the various approaches to convert an OutputStream to a Writer.

Following are the approaches to convert an OutputStream to a Writer:

OutputStream to a Writer using OutputStreamWriter

To convert an OutputStream to a Writer. We can use the OutputStreamWriter class.

  • An OutputStreamWriter class converts data from character streams to bytestreams. You can instantiate this class by passing any output stream as a parameter to one of its constructor.
  • The write() method of this class writes a single character to the underlying OutputStream object (the object that is wrapped by the current OutputStreamWriter).
  • Finally you can retrieve data from the OutputStream object and print it.OutputStreamWriter writer = new OutputStreamWriter(OutputStream out);

Example

The following program we are trying to convert the contents of a ByteArrayOutputStream to OutputStreamWriter. Here, 

  • We are passing ByteArrayOutputStream object as a parameter to the OutputStreamWriter constructor.
  • Writing a string (character array) to it using the write() method.
  • Finally we are printing the contents of the ByteArrayOutputStream using the toByteArray() method.
import java.io.*;
public class OutputStreamToWriterTest {
   public static void main(String[] args) throws Exception {
      String str = "TUTORIALSPOINT";
      System.out.println("The input string is: " + str);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      
      //using the OutputStreamWriter()
      OutputStreamWriter osw = new OutputStreamWriter(baos);
      for (int i=0; i < str.length(); i++) {
         osw.write((int) str.charAt(i));
      }
      
      osw.close();
      
      //getting the byte array and printing each byte value
      byte[] b = baos.toByteArray();
      System.out.println("The byte value of each character after converting: ");
      for (int j=0; j < b.length; j++) {
         System.out.print(b[j] + " ");
      }
   }
}

The above program produces the following output:

The input string is: TUTORIALSPOINT
The byte value of each character after converting: 
84 85 84 79 82 73 65 76 83 80 79 73 78 84

Using an OutputStreamWriter and flush() Method

This is another approach to doing the same task. In this, we use the OutputStreamWriter along with the flush() method. The flush() method of OutputStreamWriter ensures that all buffered data is written to the underlying OutputStream.

Example

The program given below converts a string "THIS IS JAVA" (OutputStream) into a byte array using OutputStreamWriter and ByteArrayOutputStream. It then prints the byte values of each character after conversion:

import java.io.*;

public class OutputStreamToWriterTest {
   public static void main(String[] args) throws IOException {
      String str = "THIS IS JAVA";
      System.out.println("The input string is: " + str);

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      //using the OutputStreamWriter()
      OutputStreamWriter osw = new OutputStreamWriter(baos);

      // Write the entire string at once
      osw.write(str);
      //using flush() method
      osw.flush();
      osw.close();

      byte[] bytes = baos.toByteArray();
      System.out.println("The byte value of each character after converting: ");
      for (byte b : bytes) {
         System.out.print(b + " ");
      }
   }
}

Following is the output of the above program:

The input string is: THIS IS JAVA
The byte value of each character after converting: 
84 72 73 83 32 73 83 32 74 65 86 65
Updated on: 2025-05-14T19:09:21+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements