Java OutputStreamWriter Class

The OutputStreamWriter class of the java.io package can be used to convert data in character form into data in bytes form.

It extends the abstract class Writer.

OutputStreamWriter
OutputStreamWriter

The OutputStreamWriter class works with other output streams. It is also known as a bridge between byte streams and character streams. This is because the OutputStreamWriter converts its characters into bytes.

For example, some characters require 2 bytes to be stored in the storage. To write such data we can use the output stream writer that converts the character into corresponding bytes and stores the bytes together.


Create an OutputStreamWriter

In order to create an OutputStreamWriter, we must import the java.io.OutputStreamWriter package first. Once we import the package here is how we can create the output stream writer.

// Creates an OutputStream
FileOutputStream file = new FileOutputStream(String path);

// Creates an OutputStreamWriter
OutputStreamWriter output = new OutputStreamWriter(file);

In the above example, we have created an OutputStreamWriter named output along with the FileOutputStream named file.

Here, we are using the default character encoding to write characters to the output stream.

However, we can specify the type of character encoding (UTF8 or UTF16) to be used to write data.

// Creates an OutputStreamWriter specifying the character encoding
OutputStreamWriter output = new OutputStreamWriter(file, Charset cs);

Here, we have used the Charset class to specify the type of character encoding.


Methods of OutputStreamWriter

The OutputStreamWriter class provides implementations for different methods present in the Writer class.

write() Method

  • write() - writes a single character to the writer
  • write(char[] array) - writes the characters from the specified array to the writer
  • write(String data) - writes the specified string to the writer

Example: OutputStreamWriter to write data to a File

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

public class Main {

  public static void main(String args[]) {

    String data = "This is a line of text inside the file.";

    try {
      // Creates a FileOutputStream
      FileOutputStream file = new FileOutputStream("output.txt");

      // Creates an OutputStreamWriter
      OutputStreamWriter output = new OutputStreamWriter(file);

      // Writes string to the file
      output.write(data);

      // Closes the writer
      output.close();
    }

    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

In the above example, we have created an output stream reader using the file output stream. The output stream reader is linked with the output.txt file.

FileOutputStream file = new FileOutputStream("output.txt");
OutputStreamWriter output = new OutputStreamWriter(file);

To write data to the file, we have used the write() method.

Here, when we run the program, the output.txt file is filled with the following content.

This is a line of text inside the file.

getEncoding() Method

The getEncoding() method can be used to get the type of encoding that is used to write data to the output stream. For example,

import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.io.FileOutputStream;

class Main {
  public static void main(String[] args) {

    try {
      // Creates an output stream
      FileOutputStream file = new FileOutputStream("output.txt");

      // Creates an output stream reader with default encoding
      OutputStreamWriter output1 = new OutputStreamWriter(file);

      // Creates an output stream reader specifying the encoding
      OutputStreamWriter output2 = new OutputStreamWriter(file, Charset.forName("UTF8"));

      // Returns the character encoding of the output stream
      System.out.println("Character encoding of output1: " + output1.getEncoding());
      System.out.println("Character encoding of output2: " + output2.getEncoding());

      // Closes the reader
      output1.close();
      output2.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

The character encoding of output1: Cp1252
The character encoding of output2: UTF8

In the above example, we have created 2 output stream writer named output1 and output2.

  • output1 does not specify the character encoding. Hence the getEncoding() method returns the default character encoding.
  • output2 specifies the character encoding, UTF8. Hence the getEncoding() method returns the specified character encoding.

Note: We have used the Charset.forName() method to specify the type of character encoding. To learn more, visit Java Charset (official Java documentation).


close() Method

To close the output stream writer, we can use the close() method. Once the close() method is called, we cannot use the writer to write the data.


Other methods of OutputStreamWriter

Method Description
flush() forces to write all the data present in the writer to the corresponding destination
append() inserts the specified character to the current writer

To learn more, visit Java OutputStreamWriter (official Java documentation).

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges