Reading Writing Text  «Prev  Next»
Lesson 1

Introducing Readers and Writers

This module introduces you to reading and writing text in Java programs.
You will learn about:
  1. The vagaries of different character sets used both on different platforms and in different cultures
  2. The Reader and Writer classes, and how to connect readers and writers to input and output streams

Reader classes are similar to input streams, and writer classes are similar to output streams. Reader classes descend from the abstract Reader class, whereas the Writer classes descend from the abstract Writer class.

Java Readers and Writers:
I. Readers consisting of BufferedReader, LineNumberReader, CharArrayReader, FileReader, InputStreamReader, FileReader
II. Writers consisting of OutpuStreamWriter, PipedWriter, StringWriter, PrintWriter

Both readers and writers are divided into low-level and high-level classes. Low-level classes communicate with I/O devices, and high-level classes communicate with the low-level ones.
Readers and writers are designed specifically for Unicode characters and low-level readers and writers deal with chars. The package provides the following low-level Reader classes:
  1. FileReader
  2. CharArrayReader
  3. PipedReader
  4. StringReader
  1. FileReader: The FileReader class is used to read streams of characters from a file. This class is useful to read text files.
  2. CharArrayReader: The CharArrayReader class reads arrays of characters by implementing a character buffer. The character array acts like a character input stream.
  3. PipedReader: The PipedReader class provides a piped character-input stream. It should be used with a piped character-output stream so that data written to the PipedWriter will be available from this reader.
  4. StringReader: The StringReader class uses strings as its source of a character stream. Individual characters can be marked and read from the string.

The high-level reader classes include
  1. BufferedReader
  2. FilterReader
  3. InputStreamReader
  1. BufferedReader: The BufferedReader class is used to read text from a character-input stream. You can use the class to improve the efficiency of your code. Buffers enable you to write and read data in bulk. It is recommended to always use buffered I/O.
  2. FilterReader: The FilterReader class is an abstract class that is used to filter character streams. By overriding the appropriate methods of FilterReader, a subclass can decide what gets read and how it is handled. For example, you can filter lines from a file, based on a regular expression.
  3. InputStreamReader: The InputStreamReader is a class that is used to convert a byte stream into a set of characters, using a specified Charset. You can use InputStreamReader to accept input from System.In, up to a designated escape character or sequence.