Skip to content
Sahithyan's S2
Sahithyan's S2 — Program Construction

Streams

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.

Types

Based on data format

Byte-oriented

Handles data in raw binary format. Used for byte by byte input and output.

ClassDescription
InputStream, OutputStreamAbstract superclass for all byte-oriented input & output streams. Used to read from or write bytes
FileInputStreamReads raw bytes from a file. Useful for reading binary data like images or audio files.
FileOutputStreamWrites raw bytes to a file. Useful for writing binary data.
BufferedInputStreamAdds buffering to an InputStream for efficient reading of bytes. Reduces the number of I/O operations by reading chunks of data at a time.
BufferedOutputStreamAdds buffering to an OutputStream for efficient writing of bytes. Reduces the number of I/O operations by writing chunks of data at a time.
ByteArrayInputStreamReads bytes from a byte array in memory. Useful for testing or manipulating byte data without involving external resources.
ByteArrayOutputStreamWrites bytes to a byte array in memory. Useful for temporary storage and manipulation of byte data.
DataInputStreamReads primitive data types (e.g., int, float) and strings from an underlying InputStream. Useful for reading structured binary data.
DataOutputStreamWrites primitive data types (e.g., int, float) and strings to an underlying OutputStream. Useful for writing structured binary data.
ObjectInputStreamReads serialized objects from an underlying InputStream. Useful for deserializing objects in Java.
ObjectOutputStreamWrites serialized objects to an underlying OutputStream. Useful for serializing objects in Java.
FilterInputStreamAbstract class for creating input streams that filter data as it is read. Subclasses include BufferedInputStream and DataInputStream.
FilterOutputStreamAbstract class for creating output streams that filter data as it is written. Subclasses include BufferedOutputStream and DataOutputStream.

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();
}
}
}
ClassDescription
Reader, WriterAbstract superclass for all character stream inputs and outputs. Used for reading and writing text data.
FileReader, FileWriterReads or writes characters from a file, using the system’s default character encoding.
BufferedReader, BufferedWriterAdds buffering to a Reader for efficient character reading or writing.
InputStreamReaderBridge from byte streams (InputStream) to character streams (Reader). Decodes bytes into characters. Specify encoding for international text.
OutputStreamWriterBridge from character streams (Writer) to byte streams (OutputStream). Encodes characters into bytes. Specify encoding for international text.
PrintWriterWrites 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, CharArrayWriterReads 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

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:

  • FileInputStream for reading from files.
  • ByteArrayInputStream for reading from byte arrays.
  • BufferedInputStream for buffering input to improve performance.

Common subclasses of Reader include:

  • FileReader
  • CharArrayReader
  • BufferedReader

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:

  • FileOutputStream for writing to files.
  • ByteArrayOutputStream for writing to byte arrays.
  • BufferedOutputStream for buffering output to improve performance.

Common subclasses of Writer include:

  • FileWriter
  • CharArrayWriter
  • BufferedWriter

Based on connection

Connection stream

A stream that connects to a source or destination. A file or socket, or a network connection.

Examples:

  • FileInputStream
  • FileOutputStream

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

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.

numbers.txtoutput.txtcontains a list of numberseach number is in separate linesfilter the lines having a non-integermultiplies each number by 2FileReaderIntegerFilterStreamDoublingStreamFileWriterConnection streamChain stream