This module introduces you to reading and writing text in Java programs.
You will learn about:
The vagaries of different character sets used both on different platforms and in different cultures
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.
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 java.io package provides the following low-level Reader classes:
FileReader: The FileReader class is used to read streams of characters from a file. This class is useful to read text files.
CharArrayReader: The CharArrayReader class reads arrays of characters by implementing a character buffer.
The character array acts like a character input stream.
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.
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
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.
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.
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.