What is I/O Filter in Java?

Similar to I/O streams, Filter streams serve the purpose of data manipulation when reading from an underlying stream. The specific type of filtering performed by these streams varies according to their nature. In a readable filter stream, the read method retrieves input from the underlying stream, applies the necessary filters, and delivers the filtered data to the invoking entity. Conversely, a writable filter stream employs the write method to filter the data before writing it to the underlying stream.

The Java.io.FilterOutputStream class serves as the superclass for output stream classes that perform filtering operations. These filtering operations can be found in subclasses such as FilterInputStream and FilterOutputStream for byte streams, and FilterReader and FilterWriter for character streams. The java.io package offers a variety of filter streams that are subclasses of FilterInputStream, including the following examples:

  1. DataInputStream and DataOutputStream
  2. BufferedInputStream and BufferedOutputStream
  3. LineNumberInputStream
  4. PushbackInputStream
  5. PrintStream

By utilizing these streams, there is no requirement to convert data from byte to char when writing to a file. These streams are considered more robust and capable compared to other streams available in Java.