Byte streams and Character streams

Byte streams and Character streams


Byte streams Character streams

File copying is done, in JDK 1.0, with byte streams. Byte streams can read or write the files containing ASCII characters that range from 0 to 255. That is, byte streams can copy the files containing English letters only but not of other languages.

If the file contains other than English characters, as Java supports Unicode characters, byte streams fail to do the job. To overcome this, in JDK 1.1 version, designers introduced character streams. Character streams operate on Unicode characters. That is, character streams can read, write and copy the files containing other than English characters. Characters streams can do with English characters also as ASCII code is a subset of Unicode.

Byte streams and Character streams

All the classes supporting byte streams can be divided into input stream classes and output stream classes. The main difference depends on the job they perform. All input stream classes open the file in read mode and all output stream classes open the file in write mode. Similarly, all character stream classes can be divided into reader classes (opens in read mode) and writer classes (opens in write mode). That is, the job of input stream classes and reader classes and also the job of output stream classes and writer classes is the same; but they belong to different divisions.

Input Stream Hierarchy

The input stream classes is not a single class but a bunch of classes doing the same job of opening the file in read mode, but they cater to different needs of the programmer. All the classes exist in java.io package and following hierarchy gives the list of classes.

Byte streams and Character streams

As you can observe, the hierarchy contains 12 input stream classes. All the classes can be divided into two categories – high-level streams and low-level streams. All the subclasses of FilterInputStream are known as high-level streams (there exists 4) and all the remaining are known as low-level streams (there are 8). One stream can be linked to another to have higher functionality, but subject to some rules explained later.

OutputStream Hierarchy

Similar to input stream hierarchy, there exists output stream hierarchy also dealing with all output stream classes.

Byte streams and Character streams

All the subclasses of FilterOutputStream are known as high-level streams (there exists 3) and all the remaining are known as low-level streams (there are 6). One stream can be linked (or chained) to another to have higher functionality.

2 thoughts on “Byte streams and Character streams

Leave a Reply

Your email address will not be published. Required fields are marked *