Java I/O: The PrintStream Class and
Character-Oriented Stream Classes The
is a very convenient class that has the ability to print representations of
various data values, such as all primitive types. During printing it converts
all characters into bytes using the platform’s default character encoding.
What is character encoding?
You must have heard about Morse Code, which has been in use worldwide for a long
time. Morse Code maps textual information as a series of on/off tones, lights,
or clicks. The skilled listener or observer interprets the original text
without any special equipment. This is called character encoding. Several character
encoding standards are in place today, including ISO 8859-1 (Western Europe), ISO
8859-2 (Western and Central Europe), ISO 8859-8 (Hebrew), and HKSCS (Hong Kong).
Unicode, which you have certainly heard of, is one such character encoding.
Java supports a variety of encoding standards. Such encoding systems map each
character in a given repertoire to a certain sequence of natural numbers,
octets, or electrical pulses to facilitate the data transmission of textual
information through networks and stores them in computers.
PrintStream class has several versions of an overloaded print method that accept
a primitive data type as an argument. These methods include :
corresponding println methods are also provided for our convenience; these
print a newline at the end.
class also provides a C-style printf method that includes the
format specifiers for the various data types in its placeholders.
other output streams, this class does not throw an IOException. Calling its checkError method checks for
exceptions. Optionally, a PrintStream can be created to flush automatically. For
this, we would use the following constructor:
out, Boolean autoFlush)
autoFlush is enabled, the flush method will be automatically invoked after a
byte array is written, a newline character (or '\n') is written, or one of the
println methods is invoked.
The System.out object we’ve used several times so far for outputting to the
console is of type PrintStream.
Character-Oriented Stream Classes
the name suggests, character-oriented stream classes operate on characters.
Java uses Unicode to store strings. You’ll recall that Unicode is one of
the encoding formats; Java supports a wide variety of encoding formats, all of
which can be configured when reading/writing textual information with the
CharArray Reader/Writer Classes
previous posts, we studied the ByteArray input/output classes,
which operate on binary data. The corresponding equivalents of these classes
that operate on character data are the
their binary counterparts, these classes operate on a buffer; however, this
time the buffer is a character buffer rather than a byte
that a character in Java consists of two bytes. Thus, each element of the
character array takes up two bytes of space to represent a given character.
Except for this difference, the two types of classes—ByteArray and CharArray—provide
very similar functionality to each other.
ByteArrayOutputStream class, the CharArrayWriter class implements a character
buffer that can grow dynamically. The CharArrayReader creates a reader on the
existing buffer to read the characters stored in it. We have previously used
the String class. This class provides an important method called toCharArray that returns a character
array containing the elements of the string. Using this method, we will be able
to construct an instance of CharArrayReader as follows:
CharArrayReader reader = new
an instance of CharArrayReader is obtained, we can use it for accessing the
individual characters of the string to perform further operations on it.