Streams in Java represent an ordered sequence of data. Java performs input and output operations in the terms of streams.
It uses the concept of streams to make I/O operations fast. For example, when we read a sequence of bytes from a binary file, actually, we’re reading from an input stream.
Similarly, when we write a sequence of bytes to a binary file, we’re writing to an output stream.
Byte Streams and Character Streams
Modern versions of Java platform define two types of I/O streams:
- Byte streams
- Character streams
Byte streams in Java are designed to provide a convenient way for handling the input and output of bytes (i.e., units of 8-bits data). They are used for reading or writing to binary data I/O.
They are especially used when we are working with binary files such as executable files, image files, and files in low-level file formats such as .zip, .class, .obj, and .exe.
Binary files are those files that are machine-readable. For example, a Java class file is an extension of “.class” and cannot be read by humans.
It can be processed by low-level tools such as a JVM (executable java.exe in Windows) and java disassembler (executable javap.exe in Windows).
Another realtime example is storing a photo in a .bmp or .jpeg file. These files are certainly not human-readable. They can only be processed by photo editing or image manipulation software.
Byte streams that are used for reading are called input streams and for writing are called output streams. They are represented by the abstract classes of InputStream and OutputStream in Java.
Character streams in Java are designed for handling the input and output of characters. They use 16-bit Unicode characters.
Character streams are more efficient than byte streams. They are mainly used for reading or writing to character or text-based I/O such as text files, text documents, XML, and HTML files.
Text files are those files that are human-readable. For example, a .txt file that contains human-readable text. This file is created with a text editor such as Notepad in Windows.
Character streams that are used for reading are called readers and for writing are called writers. They are represented by the abstract classes of Reader and Writer in Java.
Stream Classes in Java
All streams in Java are represented by classes in java.io package. This package contains a lot of stream classes that provide abilities for processing all types of data.
These stream classes can be classified into two basic groups based on the date type. They are as:
- Byte Stream Classes (support for handling I/O operations based on bytes)
- Character Stream Classes (support for managing I/O operations on characters)
Let’s understand Byte stream classes and character stream classes one by one.
Byte Stream Classes in Java
There are two kinds of byte stream classes in Java. They are as follows:
- InputStream classes
- OutputStream classes
The hierarchy diagram of the classification of byte stream classes is shown in the above figure.
InputStream Classes in Java
InputStream class is an abstract class. It is the root class for reading binary I/O data. It is the superclass of all classes representing an input stream of bytes.
Since InputStream class is an abstract class, we cannot create an object of this class. We must use subclasses of this class to create an object.
The several subclasses of Java InputStream class can be used for performing several input functions. They are listed with a brief description in the below table:
|BufferedInputStream||It adds buffering abilities to an input stream. In simple words, it buffered input stream.|
|ByteArrayInputStream||Input stream that reads data from a byte array.|
|DataInputStream||It reads bytes from the input stream and converts them into appropriate primitive-type values or strings.|
|FileInputStream||This input stream reads bytes from a file.|
|FilterInputStream||It filters bytes from an input stream.|
|ObjectInputStream||Input stream for objects|
|PipedInputStream||It creates a communication channel on which data can be received.|
|PushbackInputStream||It is a subclass of FilterInputStream that adds “pushback” functionality to an input stream.|
|SequenceInputStream||It is used to read input streams sequentially, one after the other.|
InputStream Methods in Java
The abstract InputStream class in Java defines several methods for performing input functions such as reading bytes, closing streams, marking positions in streams, etc.
All these methods are present in java.io.InputStream package. InputStream methods are as follows:
1. int Read(): The read() method reads the next byte of data from the input stream. It returns input byte read as an int value in the range 0 to 255.
If no byte is present because the end of the stream is reached, the value –1 is returned. If this method encounters an I/O error, it will throw an IOException.
The read() method returns an int value -1 instead of a byte because it indicates the end of stream.
2. int read(byte[ ] b): This method reads the number of bytes from the input stream and stores them into the array of bytes b.
It returns the total number of bytes read as an int. If the end of stream is reached, returns -1.
3. int read(byte[ ] b, int n, int m): It reads up to m bytes of data from the input stream starting from nth byte into an array b.
It returns the total number of bytes read as an int. Returns -1 at the end of stream because of no more data.
4. int available(): The available method returns an estimate of the number of bytes that can be read (or skipped over) from the input stream.
5. void close(): The close() method closes the input stream and releases any system resources associated with it.
6. long skip(long n): This method skips over n bytes of data from this input stream. It returns the actual number of bytes skipped.
7. void reset(): The reset() method is used to go back to the beginning of the stream.
8. boolean markSupported(): This method tests this input stream supports the mark and reset methods. It returns true if the input stream supports mark and reset methods.
OutputStream Classes in Java
OutputStream class is an abstract class. It is the root class for writing binary data. It is a superclass of all classes that represents an output stream of bytes.
Since like InputStream, OutputStream is an abstract class, therefore, we cannot create object of it. The hierarchy of classification of OutputStream classes is shown in the above diagram.
The several subclasses of OutputStream class in Java can be used for performing several output functions. They are listed with a brief description in the below table:
|BufferedOutputStream||It adds buffering abilities to an output stream. In simple words, it buffered output stream.|
|ByteArrayOutputStream||Output stream that writes data to a byte array.|
|DataOutputStream||It converts primitive-type values or strings into bytes and outputs bytes to the stream.|
|FileOutputStream||It writes byte stream into a file.|
|FilterOutputStream||It filters bytes from an output stream.|
|ObjectOutputStream||Output stream for objects|
|PipedOutputStream||It creates a communication channel on which data can be sent.|
OutputStream Methods in Java
The OutputStream class defines the following method in java.io.OutputStream package that are used to perform output tasks such as writing bytes, closing streams, and flushing streams.
The OutputStream methods with a brief description are as follows:
1. void write(int b): The write() method writes the specified byte to the output stream. It accepts an int value as an input parameter. It throws an IOException if an I/O error occurs (e.g. output stream has been closed).
2. void write(byte[ ] b): This method writes all the specified bytes in the array b to the output stream.
3. void write(byte[ ] b, int n, int m): It writes m bytes from array b starting from nth byte to the output stream.
4. void close(): It closes the output stream and releases any system resources associated with this stream.
5. void flush(): It flushes the output stream and forces any buffered output bytes to be written out.
Hope that this tutorial has covered almost all the important points and related topics concerning stream classes in Java. I hope that you will have understood the basic points of byte stream classes in Java.
In the next tutorial, we will learn character stream classes in Java.
Thanks for reading!!!
Next ⇒ CharacterStream Classes in Java