Java - IO - Character Stream

> Procedural Languages > Java > Java - IO (Input/Output) - (File System|File|Console)

1 - About

Character streams are build above byte stream. They decodes bytes into characters (or the other way around) using a specified charset.

The Java platform stores character values using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set (which facilitates internationalization)

Advertising

3 - Superclass

All characters stream classes are descended from

  • Reader (to read characters). All implementations extends the Reader abstract class.
  • Writer (to write characters). All implementations extends the Writer abstract class.

The Reader and Writer types handle the conversion of the encoded characters to and from bytes.

4 - Implementation

Character streams are often “wrappers” for byte streams. Character streams:

  • use the byte stream to perform the physical I/O,
  • and handle translation between characters and bytes.

5 - Streams Type

5.1 - Base

Most of the below streams are based on this two base streams.

Base Stream Operations Logical unit Description
FileReader Byte Read Bytes FileReader reads bytes from an FileInputStream and decodes them as UTF-8 multi-byte characters. See byte stream
FileWriter Byte Write Bytes Same as FileRead but with FileOutputStream. See byte stream

Each read or write request is handled directly by the underlying OS.

FileReader and FileWriter use an int variable to read to and write from.

inputStream = new FileReader(Parameters.FILE_PATH_READ);
int c = in.read();

It holds a character value in its last 16 bits (in the case of byte stream, it holds a byte in its last 8 bytes).

To handle byte IO,

Advertising

5.2 - Buffer

The buffer streams add a buffer functionality in order to reduce the I/O requests:

Base Stream Operations Operation unit Description
CharArrayReader Char Array Read Char, Array This class implements a character buffer that can be used as a character-input stream.
CharArrayWriter Char Array Write Char, Array This class implements a character buffer that can be used as an Writer.
BufferedReader Wrap with a buffer Byte Stream Char, Array, Line Reads text normally from a Reader (normally from a FileReader) stream, buffering characters.
BufferedWriter Wrap with a buffer Byte Stream Char, Char Array, LineSeparator Writes text to a character-output stream, buffering characters.
BufferedReader inputStream = new BufferedReader(new FileReader("File/Path");
BufferedWriter outputStream = new BufferedWriter(new FileWriter("File/Path"));

5.3 - Line and String

Wrapper Stream Operations Description
PrintStream Write by printing (ln, f) A PrintStream adds functionality to print data conveniently (Println, printf, format). PrintStream is technically a byte stream but it utilizes an internal character stream object to emulate many of the features of character streams.
PrintWriter Write by printing (ln, f) Prints formatted representations of objects to a text-output stream.
LineNumberReader Read Line A buffered character-input stream that keeps track of line numbers. A BufferedReader can also read line
StringReader Read String A character stream whose source is a string.
StringWriter Write String A character stream that collects its output in a string buffer, which can then be used to construct a string.

Example:

PrintWriter outputStream = new PrintWriter(new FileWriter(Parameters.FILE_PATH_WRITE));
outputStream.println("Add a line");
Advertising

5.4 - Pipe (Stream Connection)

Wrapper Stream Operations Description
PipedReader Pipe Piped character-input streams. A piped input stream should be connected to a piped output stream.
PipedWriter Pipe Piped character-output streams.

5.5 - Tokenizer / Scanner (With Push Back)

Wrapper Stream Operations Description
StreamTokenizer Tokenize The StreamTokenizer class takes an input stream and parses it into “tokens”, allowing the tokens to be read one at a time.
PushbackInputStream Push Back A PushbackInputStream adds functionality to another input stream, namely the ability to “push back” or “unread” one byte.
PushbackReader Push Back A character-stream reader that allows characters to be pushed back into the stream.

5.6 - Bridge to Byte and From Byte Stream

Wrapper Stream Operations Description
OutputStreamWriter Bridge with Byte Stream An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset.
InputStreamReader Bridge with Byte Stream An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset.