public void close() Method

Advertisements


Description

The java.io.FilterInputStream.close() method closes this input stream and releases any system resources associated with the stream.

Declaration

Following is the declaration for public void close() method:

public void close()

Parameters

  • NA

Return Value

The method does not return any value.

Exception

  • IOException -- If an I/O error occurs.

Example

The following example shows the usage of public void close() method.

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FilterInputStreamDemo {
   public static void main(String[] args) throws Exception {
      
      InputStream is = null; 
      FilterInputStream fis = null; 
      
      try{
         // create input streams
         is = new FileInputStream("C://test.txt");
         fis = new BufferedInputStream(is);
         
         // closes and releases the associated system resources
         fis.close();
         
         // read is called after close() invocation
         fis.read();
         
      }catch(IOException e){
         
         System.out.print("stream is closed prior ot this call");
      }finally{
         
         // releases any system resources associated with the stream
         if(is!=null)
            is.close();
         if(fis!=null)
            fis.close();
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program:

ABCDEF

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

stream is closed prior ot this call



Advertisements
Advertisements