Java IO: Readers and Writers

Jakob Jenkov
Last update: 2015-09-04

The Java Reader (java.io.Reader) and Java Writer class (java.io.Writer) in Java IO work much like the InputStream and OutputStream with the exception that Reader and Writer are character based. They are intended for reading and writing text. The InputStream and OutputStream are byte based, remember?

Reader

The Java Reader is the base class of all Reader's in the Java IO API. Subclasses include a BufferedReader, PushbackReader, InputStreamReader, StringReader and several others.

Here is a simple Java IO Reader example:

    Reader reader = new FileReader("c:\\data\\myfile.txt");

    int data = reader.read();
    while(data != -1){
        char dataChar = (char) data;
        data = reader.read();
    }

Notice, that while an InputStream returns one byte at a time, meaning a value between 0 and 255 (or -1 if the stream has no more data), the Reader returns a char at a time, meaning a value between 0 and 65535 (or -1 if the stream has no more data). This does not necessarily mean that the Reader reads two bytes at a time from the source it is connected to. It may read one or more bytes at a time, depending on the encoding of the text being read.

Combining Readers With InputStreams

A Java Reader can be combined with an InputStream. If you have an InputStream and want to read characters from it, you can wrap it in an InputStreamReader. Pass the InputStream to the constructor of the InputStreamReader like this:

Reader reader = new InputStreamReader(inputStream);

In the constructor you can also specify what character set to use to decode the text etc. More on that in the text on InputStreamReader.

Writer

The Java Writer class is the base class of all Writers in the Java IO API. Subclasses include BufferedWriter and PrintWriter among others.

Here is a simple Java IO Writer example:

Writer writer = new FileWriter("c:\\data\\file-output.txt");

writer.write("Hello World Writer");
writer.close();

Combining Writers With OutputStreams

A Java Writer can be combined with an OutputStream just like Readers and InputStream's. Wrap the OutputStream in an OutputStreamWriter and all characters written to the Writer are passed on to the OutputStream. Here is an OutputStreamWriter example:

Writer writer = new OutputStreamWriter(outputStream);

Combining Readers and Writers

Just like with streams, Reader's and Writer's can be combined into chains to achieve more interesting IO. It works just like combining the Reader with InputStream's or the Writer with OutputStream's. For instance, you can achieve buffering by wrapping a Reader in a BufferedReader, or a Writer in a BufferedWriter. Here are two such examples:

Reader reader = new BufferedReader(new FileReader(...));

Writer writer = new BufferedWriter(new FileWriter(...));

Jakob Jenkov

Featured Videos

Java ConcurrentMap + ConcurrentHashMap

Java Generics

Java ForkJoinPool

P2P Networks Introduction

















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next