Tech and Media Labs
This site uses cookies to improve the user experience.




Java IO: OutputStreamWriter

Jakob Jenkov
Last update: 2015-09-05

The Java OutputStreamWriter class (java.io.OutputStreamWriter) is intended to wrap an OutputStream, thereby turning the byte based output stream into a character based Writer.

The Java OutputStreamWriter is useful if you need to write characters to a file, encoded as e.g. UTF-8 or UTF-16. You can then write the characters (char values) to the OutputStreamWriter and it will encode them correctly and write the encoded bytes to the underlying OutputStream.

OutputStreamWriter Example

Here is a simple Java OutputStreamWriterexample:

OutputStream outputStream       = new FileOutputStream("c:\\data\\output.txt");
Writer       outputStreamWriter = new OutputStreamWriter(outputStream);

outputStreamWriter.write("Hello World");

outputStreamWriter.close();

Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.

Character Encoding Constructors

The Java OutputStreamWriter also has alternative constructors that allow you to specify the character set (ISO-Latin1, UTF-8, UTF-16 etc.) to use to convert the written characters to the bytes written to the underlying OutputStream. Here is a Java OutputStreamWriter example showing the use of one of these constructors:

OutputStream outputStream       = new FileOutputStream("c:\\data\\output.txt");
Writer       outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");

This example creates an OutputStreamWriter that will convert all characters written to it to UTF-8 encoded characters (one or more bytes per character) and write the UTF-8 encoded bytes to the underlying OutputStream .

Closing an OutputStreamWriter

When you are finished writing characters to the OutputStreamWriter you should remember to close it. Closing an OutputStreamWriter will also close the OutputStream instance to which the OutputStreamWriter is writing.

Closing an OutputStreamWriter is done by calling its close() method. Here is how closing an OutputStreamWriter looks:

outputStreamWriter.close();

You can also use the try-with-resources construct introduced in Java 7. Here is how to use and close a OutputStreamWriter looks with the try-with-resources construct:

OutputStream output = new FileOutputStream("data/data.bin");

try(OutputStreamWriter outputStreamWriter =
    new OutputStreamWriter(output)){

    Person person = new Person();
    person.name = "Jakob Jenkov";
    person.age  = 40;

    outputStreamWriter.writeObject(person);
}

Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.

Notice also that the first FileOutputStream instance is not created inside the try-with-resources block. That means that the try-with-resources block will not automatically close this FileOutputStream instance. However, when the OutputStreamWriter is closed it will also close the OutputStream instance it writes to, so the FileOutputStream instance will get closed when the OutputStreamWriter is closed.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC