in , ,

Java OutputStreamWriter Class

Java OutputStreamWriter Class
Java OutputStreamWriter Class

In this tutorial, we will learn about Java OutputStreamWriter and its techniques with the help of examples.

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

It extends the abstract class Writer.

The OutputStreamWriter class works with other output streams. It is otherwise called a bridge between byte streams and character streams. This is on the grounds that the OutputStreamWriter changes over its characters into bytes.

For instance, a few characters require 2 bytes to be stored in the storage. To write such data we can use the output stream writer that changes over the character into comparing bytes and stores the bytes together.


Make an OutputStreamWriter

To make an OutputStreamWriter, we should import the java.io.OutputStreamWriter package first. When we import the package here is the means by which we can make 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 made an OutputStreamWriter named output alongside the FileOutputStream named record.

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

However, we can determine the sort 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 made an output stream reader using the file output stream. The output stream reader is connected with the output.txt record.

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

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

Here, when we run the program, the output.txt record is loaded up with the accompanying substance.

This is a line of text inside the file.

getEncoding() Method

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

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 made 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() strategy. When the close() technique is called, we can’t use the writer to write the data.


Other methods of OutputStreamWriter

MethodDescription
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).


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java InputStreamReader Class

Java InputStreamReader Class

Java FileReader Class

Java FileReader Class