Java - FilterOutputStream close() method



Description

The Java FilterOutputStream close() method closes the output stream and releases system resources. Once closed, the stream cannot be written to again. Flushes any remaining buffered data before closing. Releases file handles to prevent resource leaks. No further writes allowed after closing. Commonly used with BufferedOutputStream, DataOutputStream, etc.

Declaration

Following is the declaration for java.io.FilterOutputStream.close() method −

public void close()

Parameters

NA

Return Value

This method does not return any value.

Exception

  • IOException − If any I/O error occurs.

Example - Usage of FilterOutputStream close() method

The following example shows the usage of Java FilterOutputStream close() method.

FilterOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FilterOutputStreamDemo {
   public static void main(String[] args) throws Exception {
      OutputStream os = null; 
      FilterOutputStream fos = null;
      
      try {
         // create input streams
         os = new FileOutputStream("test.txt");
         fos = new FilterOutputStream(os);

         // releases any system resources associated with the stream
         fos.close();
         
         // writes byte to the output stream
         fos.write(65);
         
      } catch(IOException e) {
         // if any I/O error occurs
         System.out.print("Close() is invoked prior to write()");
      } finally {
         // releases any system resources associated with the stream
         if(os!=null)
            os.close();
         if(fos!=null)
            fos.close();
      }
   }
}

Output(assuming test.txt contains ABCDEF)

Let us compile and run the above program, this will produce the following result−

Close() is invoked prior to write()

Example - Using close() with BufferedOutputStream

The following example shows the usage of Java FilterOutputStream close() method.

FilterOutputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamDemo {
   public static void main(String[] args) {
      try (FilterOutputStream fos = new BufferedOutputStream(new FileOutputStream("output.txt"))) {
         String data = "Hello, FilterOutputStream!";
         fos.write(data.getBytes()); // Write data to file
         fos.flush(); // Ensure data is written before closing

         System.out.println("Data written successfully.");
      } catch (IOException e) {
         e.printStackTrace();
      }
      // Stream is automatically closed due to try-with-resources
   }
}

Output(assuming example.txt contains Hello, FilterOutputStream!)

Let us compile and run the above program, this will produce the following result−

Data written successfully.

Explanation

  • Uses BufferedOutputStream, a subclass of FilterOutputStream.

  • Writes string data to "output.txt".

  • Flushes data to ensure it is written before closing.

  • Uses try-with-resources, which automatically calls close().

Example - Manually Closing DataOutputStream (A FilterOutputStream Subclass)

The following example shows the usage of Java FilterOutputStream close() method.

FilterOutputStreamDemo.java

package com.tutorialspoint;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;

public class FilterOutputStreamDemo {
   public static void main(String[] args) {
      FilterOutputStream fos = null;
      try {
         fos = new DataOutputStream(new FileOutputStream("data.bin"));
         fos.write(65); // Write ASCII 'A' (binary data)
         fos.write(66); // Write ASCII 'B'

         System.out.println("Data written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         try {
            if (fos != null) {
               fos.close(); // Manually closing the stream
               System.out.println("Stream closed successfully.");
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Data written to file.
Stream closed successfully.

Explanation

  • Uses DataOutputStream, which extends FilterOutputStream.

  • Writes binary data (65 → 'A', 66 → 'B') to "data.bin".

  • Manually closes the stream inside finally to ensure resources are freed.

  • Prevents memory leaks by checking if fos != null before closing.

java_io_filteroutputstream.htm
Advertisements