articles

Home / DeveloperSection / Articles / Java I/O: The PrintStream Class and Character-Oriented Stream Classes

Java I/O: The PrintStream Class and Character-Oriented Stream Classes

Samuel Fernandes1799 26-May-2016

This 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.

The PrintStream class has several versions of an overloaded print method that accept a primitive data type as an argument. These methods include :

·         print (boolean)

·         print (char)

·         print (double)

The corresponding println methods are also provided for our convenience; these print a newline at the end.

The class also provides a C-style printf method that includes the format specifiers for the various data types in its placeholders.

Unlike 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:

PrintStream (OutputStream out, Boolean autoFlush)

If 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.

Note, The System.out object we’ve used several times so far for outputting to the console is of type PrintStream.

The Character-Oriented Stream Classes

As 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 reader/writer classes..

The CharArray Reader/Writer Classes

In 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

·         CharArrayReader class

·         CharArrayWriter class

Like their binary counterparts, these classes operate on a buffer; however, this time the buffer is a character buffer rather than a byte buffer.

Note 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.

Like 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 CharArrayReader(str.toCharArray());

Once an instance of CharArrayReader is obtained, we can use it for accessing the individual characters of the string to perform further operations on it.


Updated 18-Dec-2017

Leave Comment

Comments

Liked By