FilterOutputStream in Java | Methods, Example

FilterOutputStream in Java is a concrete subclass class of OutputStream class that filters data of an underlying output stream.

It sits on top of an already existing underlying output stream.

Java FilterOutputStream class implements Closeable, Flushable, and AutoCloseable interfaces.

FilterInputStream class has four important subclasses that are as follows:

  • BufferedOutputStream,
  • CheckedOutputStream,
  • DataOutputStream,
  • DeflaterOutputStream,
  • DigestOutputStream,
  • InflaterOutputStream,
  • PrintStream

The general syntax for declaration of FilterOutputStream class is given below:

public class FilterOutputStream
   extends OutputStream
   implements Closeable, Flushable, AutoCloseable

FilterOutputStream was added in Java 1.0 version. It is present in java.io.FilterOutputStream package.

Constructor of FilterOutputStream class in Java


FilterOutputStream class provides a single protected constructor that specifies the underlying stream from which filter stream writes data.

1. FilterOutputStream​(OutputStream outputStream): This constructor creates an output stream filter built on top of the specified underlying output stream.


Since the constructor provided by filter output stream is protected, we cannot use FilterOutputStream class directly. We need to create an instance of subclasses of FilterOutputStream class to filter data.

FilterOutputStream Methods in Java


FilterOutputStream class does not define any new methods. All the methods available in filter output stream are inherited from its superclass OutputStream.

Some important inherited methods are as follows:

Method Description
1. void close​(): This method closes the output stream and releases any system resources associated with the stream.
2. void flush​(): This method flushes the output stream and forces any buffered output bytes to be written out to the stream.
3. void write​(byte[ ] b): This method writes b.length bytes to the underlying output stream.
4. void write​(byte[ ] b, int n, int m): This method writes m bytes from the specified byte array starting from the nth byte to the output stream.
5. void write​(int b): This method is used to write the specified byte to this output stream.

All the above methods will throw an exception named IOException when an error occurs.

Example Program based on FilterOutputStream


Let’s take an example program to write a string into the output stream using FilterOutputStream.

Program source code 1:

package javaProgram;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
public class BufferedOutputStreamEx {
public static void main(String[ ] args) throws IOException
{
    String filepath = "D:\\testout.txt";	
// Create a FileOutputStream object to connect text file with output stream. 
    FileOutputStream fos = new FileOutputStream(filepath);  
  
// Create a FilterOutputStream object to connect fos with filter. 
   FilterOutputStream filter = new FilterOutputStream(fos);  
    
   String s="Welcome to Java world";      
    byte b[ ]=s.getBytes(); // Converting bytes into strings.    
    filter.write(b);    
    filter.flush();  
    filter.close();  
    fos.close();  
    System.out.println("Successfully written...");  
 }
}
Output:
            Successfully written...

testout.txt file: Welcome to Java world.


Hope that this tutorial has covered almost all the important points concerning FilterOutputStream class in Java with example program. I hope that you will have understood the basic points of filter output stream.
Thanks for reading!!!
Next ⇒ DataInputStream in Java

⇐ Prev Next ⇒