Java - IO - Character Stream

Java Conceptuel Diagram

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)

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.

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.

Streams Type

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,

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"));

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");

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.

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.

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.





Discover More
Java Conceptuel Diagram
Java - IO - Buffer Streams

Without buffer, each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers: disk access, network activity,...
Java Conceptuel Diagram
Java - IO - Byte Stream

Byte I/O Operations (Stream) in java Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from this two class: input stream to read byte or...
Java Fileiomethods
Java - IO - Connection (Stream and Channel)

in Java. In order to perform I/O operations (for example reading or writing), you need to perform a connection. In Java, this connection are modelled through: a stream (java.io package) or a channel...
Java Conceptuel Diagram
Java - IO - Standard Streams

in Java. For historical reasons, Standard Streams are byte streams and not character streams System.out and System.err are defined as java/io/PrintStreamPrintStream objects. PrintStream is technically...



Share this page:
Follow us:
Task Runner