Java - FilterInputStream close() method



Description

The Java FilterInputStream close() method closes the input stream and releases system resources associated with it. Ensures no more reading can be done from the stream. Releases system resources (e.g., file handles, network connections).

Declaration

Following is the declaration for java.io.FilterInputStream.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 - Usage of FilterInputStream close() method

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

FilterInputStreamDemo.java

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 IOException {
      InputStream is = null; 
      FilterInputStream fis = null; 
      int i = 0, j = 0;
      char c;
      
      try {
         // create input streams
         is = new FileInputStream("test.txt");
         fis = new BufferedInputStream(is);
         
         // read till the end of the file
         while((i = fis.read())!=-1) {
         
            // converts integer to character
            c = (char)i;
            
            // prints
            System.out.print("Read: "+c);
            
            // number of bytes available
            j = fis.available();
            
            // prints
            System.out.println("; Available bytes: "+j);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(is!=null)
            is.close();
         if(fis!=null)
            fis.close();
      }
   }
}

Output(assuming test.txt contains ABCDEF)

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

Read: A; Available bytes: 5
Read: B; Available bytes: 4
Read: C; Available bytes: 3
Read: D; Available bytes: 2
Read: E; Available bytes: 1
Read: F; Available bytes: 0

Example - Closing a BufferedInputStream (Subclass of FilterInputStream)

The following example shows automatically closing of BufferedInputStream.

FilterInputStreamDemo.java

package com.tutorialspoint;

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

public class FilterInputStreamDemo {
   public static void main(String[] args) {
      try (FilterInputStream fis = new BufferedInputStream(new FileInputStream("example.txt"))) {
         int data;
         while ((data = fis.read()) != -1) {
            System.out.print((char) data);
         }
         // The stream is automatically closed here due to try-with-resources
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(assuming example.txt contains JavaProgramming)

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

JavaProgramming

Explanation

  • Creates a BufferedInputStream, which extends FilterInputStream, to read "example.txt".

  • Reads file contents byte by byte and prints them.

  • Uses try-with-resources, so close() is automatically called at the end.

Example - Using BufferedOutputStream (A Subclass of FilterOutputStream)

The following example shows automatically closing of BufferedOutputStream.

FilterInputStreamDemo.java

package com.tutorialspoint;

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

public class FilterInputStreamDemo {
   public static void main(String[] args) {
      try (FilterOutputStream fos = new BufferedOutputStream(new FileOutputStream("output.txt"))) {
         String data = "Hello, FilterOutputStream!";
         fos.write(data.getBytes()); // Writing data to file
         fos.flush(); // Ensures 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

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

Data written successfully.

Explanation

  • Creates a BufferedOutputStream, which extends FilterOutputStream.

  • Writes a string ("Hello, FilterOutputStream!") as bytes.

  • Uses flush() to ensure all data is written before closing.

  • Uses try-with-resources, so close() is automatically called.

java_io_filterinputstream.htm
Advertisements