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




Java IO: Writer

Jakob Jenkov
Last update: 2015-09-04

The Java Writer class (java.io.Writer) is the base class for all Writer subclasses in the Java IO API. A Writer is like an OutputStream except that it is character based rather than byte based. In other words, a Writer is intended for writing text, whereas an OutputStream is intended for writing raw bytes.

Characters in Unicode

Today, many applications use UTF (UTF-8 or UTF-16) to store text data. It may take one or more bytes to represent a single character in UTF-8. In UTF-16 each character takes 2 bytes to represent. To write UTF-8 or UTF-16 correctly you need to know which of the two formats you want to store the text in, and you need to know how to properly encode characters using the chosen format.

This is where the Java Writer class comes in handy. The Java Writer subclasses can normally handle UTF-8 and UTF-16 encoding for you, so you don't have to worry about that.

Writer Subclasses

You will normally use a Writer subclass rather than a Writer directly. Subclasses of Writer include OutputStreamWriter, CharArrayWriter, FileWriter, plus many others. For a full overview of Writer's see the Java IO Class Overview Table.

Writers and Destinations

A Java Writer is typically connected to some destination of data like a file, char array, network socket etc. This is also explained in more detail in the Java IO Overview text.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC