Java.io.FilterOutputStream.Close() Method

Advertisements


Description

The java.io.FilterOutputStream.Close() method closes the stream and release any system resources associated with the stream.

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 an I/O error occurs..

Example

The following example shows the usage of java.io.FilterOutputStream.Close() method.

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("C://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();
      }
   }
}

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

Close() is invoked prior to write()



Advertisements
Advertisements