Java-Stream Classes

Stream Classes

In Java, a stream is a path along which the data flows. Every stream has a source and a destination. We can build a complex file processing sequence using a series of simple stream operations. Two fundamental types of streams are Writing streams and Reading streams. While an Writing streams writes data into a source(file) , an Reading streams is used to read data from a source(file).

 

Types of Streams

The java.io package contains a large number of stream classes that provide capabilities for processing all types of data. These classes may be categorized into two groups based on the data type on which they operate.

  • Byte stream classes
  • Character stream classes

Text and Binary Formats of Data

There are two fundamentally different ways to store data. They are text format and binary format. Data items are stored in human-readable form in the text format. For example, the data item 85,050 is stored as the sequence of the five characters ‘8’ ‘5’ ‘0’ ‘5’ ‘0’. Data items are represented in bytes in the case of binary format. As a byte consists of 8 bits, it can represent one of 256 values. Since the data item 85,050 can be represented as the sequence of the four bytes 0, 1, 76, 58.

 

If data items are available in text format, we have to use the Character stream classes to process the input and output. If the data items are made available in binary format, we have to use the Byte stream classes. While the text format is convenient for humans, storage of data in binary format is more compact and more efficient.

Byte Stream Classes

Byte stream classes have been designed to provide functional features for creating and manipulating streams and files for reading and writing bytes. Java provides two kinds of byte stream classes: input stream classes and output stream classes.

 

Input Stream Classes

Input stream classes that are used to read bytes include a super class known as Inputstream and a number of subclasses for supporting various input-related functions. The super class InputStream is an abstract class, and, therefore, we cannot create instances of this class. Rather, we must use the subclasses that inherit from this class.

Output Stream Classes

Output stream classes are derived from the base class Outputstream like InputStream, the OutputStream is an abstract class and therefore we cannot instantiate it. The several subclasses of the OutputStream can be used for performing the output operations.

Character Stream Classes

Character streams can be used to read and write 16-bit Unicode characters. Like byte streams, there are two kinds of character stream classes, namely, reader stream classes and writer stream classes.

 

Reader Stream Classes

Reader stream classes that are used to read characters include a super class known as Reader and a number of subclasses for supporting various input-related functions. Reader stream classes are functionally very similar to the input stream classes, except input streams use bytes as their fundamental unit of information, while reader streams use characters. The Reader class contains methods that are identical to those available in the InputStream class, except Reader is designed to handle characters. Therefore, reader classes can perform all the functions implemented by the input stream classes.

Writer Stream Classes

Like output stream classes, the writer stream classes are designed to perform all output operations on files. Only difference is that while output stream classes are designed to write bytes, the writer stream are designed to write character. The Writer class is an abstract class which acts as a base class for all the other writer stream classes. This base class provides support for all output operations by defining methods that are identical to those in Outputstream class.

Leave a Comment