Stream means a continuous flow of data. Stream is an object that reads from a source or write to a destination or both. Used to output to a screen or a read from a keyboard. Used when total memory is lesser than the incoming or outgoing data. Used to process and transmit data progressively.
In Java, streams are used to perform input and output (I/O) operations.
Based on data format
Section titled “Based on data format”Byte-oriented
Section titled “Byte-oriented”Handles data in raw binary format. Used for byte by byte input and output.
| Class | Description |
|---|---|
InputStream, OutputStream | Abstract superclass for all byte-oriented input & output streams. Used to read from or write bytes |
FileInputStream | Reads raw bytes from a file. Useful for reading binary data like images or audio files. |
FileOutputStream | Writes raw bytes to a file. Useful for writing binary data. |
BufferedInputStream | Adds buffering to an InputStream for efficient reading of bytes. Reduces the number of I/O operations by reading chunks of data at a time. |
BufferedOutputStream | Adds buffering to an OutputStream for efficient writing of bytes. Reduces the number of I/O operations by writing chunks of data at a time. |
ByteArrayInputStream | Reads bytes from a byte array in memory. Useful for testing or manipulating byte data without involving external resources. |
ByteArrayOutputStream | Writes bytes to a byte array in memory. Useful for temporary storage and manipulation of byte data. |
DataInputStream | Reads primitive data types (e.g., int, float) and strings from an underlying InputStream. Useful for reading structured binary data. |
DataOutputStream | Writes primitive data types (e.g., int, float) and strings to an underlying OutputStream. Useful for writing structured binary data. |
ObjectInputStream | Reads serialized objects from an underlying InputStream. Useful for deserializing objects in Java. |
ObjectOutputStream | Writes serialized objects to an underlying OutputStream. Useful for serializing objects in Java. |
FilterInputStream | Abstract class for creating input streams that filter data as it is read. Subclasses include BufferedInputStream and DataInputStream. |
FilterOutputStream | Abstract class for creating output streams that filter data as it is written. Subclasses include BufferedOutputStream and DataOutputStream. |
Character-oriented
Section titled “Character-oriented”Handles data in the form of characters (16-bit Unicode). Used for input and output of characters. Built on top of byte oriented streams. Automatically handle character encoding, line endings. In Java, the main classes to work with character streams are:
In the below example, output file is written as the input file is being read. This approach can be used to copy a huge file as well.
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;
public class CharacterStreamExample { public static void main(String[] args) { try (FileReader fr = new FileReader("input.txt"); FileWriter fw = new FileWriter("output.txt")) { int charData; while ((charData = fr.read()) != -1) { fw.write(charData); } } catch (IOException e) { e.printStackTrace(); } }}| Class | Description |
|---|---|
Reader, Writer | Abstract superclass for all character stream inputs and outputs. Used for reading and writing text data. |
FileReader, FileWriter | Reads or writes characters from a file, using the system’s default character encoding. |
BufferedReader, BufferedWriter | Adds buffering to a Reader for efficient character reading or writing. |
InputStreamReader | Bridge from byte streams (InputStream) to character streams (Reader). Decodes bytes into characters. Specify encoding for international text. |
OutputStreamWriter | Bridge from character streams (Writer) to byte streams (OutputStream). Encodes characters into bytes. Specify encoding for international text. |
PrintWriter | Writes formatted representations of objects to a text-output stream. Offers print(), println(), printf(), and auto-flushing. Preferred over PrintStream for text output due to character handling. |
CharArrayReader, CharArrayWriter | Reads characters from or writes characters to a character array. Useful for temporary storage and manipulation of character data in memory. |
Based on direction of data flow
Section titled “Based on direction of data flow”Input stream
Section titled “Input stream”InputStream- an abstract class that represents an input stream of bytes. Provides methods to read bytes from a source.Reader
Common subclasses of InputStream include:
FileInputStreamfor reading from files.ByteArrayInputStreamfor reading from byte arrays.BufferedInputStreamfor buffering input to improve performance.
Common subclasses of Reader include:
FileReaderCharArrayReaderBufferedReader
Output stream
Section titled “Output stream”In Java:
OutputStream- an abstract class that represents an output stream of bytes. Provides methods to write bytes to a destination.Writer
Common subclasses of OutputStream include:
FileOutputStreamfor writing to files.ByteArrayOutputStreamfor writing to byte arrays.BufferedOutputStreamfor buffering output to improve performance.
Common subclasses of Writer include:
FileWriterCharArrayWriterBufferedWriter
Based on connection
Section titled “Based on connection”Connection stream
Section titled “Connection stream”A stream that connects to a source or destination. A file or socket, or a network connection.
Examples:
FileInputStreamFileOutputStream
Chain stream
Section titled “Chain stream”Aka. filtered stream, wrapper streams. A stream that reads from or writes to a connection stream. Works only if chained to other streams. Adheres to decorator pattern.
Examples:
BufferedReader
Example
Section titled “Example”Suppose there’s a numbers.txt containing a list of numbers (one per line). We want to remove non-integers first, then double the remaining numbers. And finally we want to write the results to a new file output.txt.