Home » Java programming language

Java FilterInputStream available() Method with Example

FilterInputStream Class available() method: Here, we are going to learn about the available() method of FilterInputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 02, 2020

FilterInputStream Class available() method

  • available() method is available in java.io package.
  • available() method is used to return the available bytes left that can be read from this FilterIputStream without blocking by the next call of this method for this FilterInputStream.
  • available() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • available() method may throw an exception at the time of returning left bytes to be read.
    IOException: This exception may throw when getting any input/output error while performing.

Syntax:

    public int available();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is int, it returns an approximate number of available bytes that can be read.

Example:

// Java program to demonstrate the example 
// of int available() method of FilterInputStream

import java.io.*;

public class AvailableOfFIS {
 public static void main(String[] args) throws Exception {
  FileInputStream fis_stm = null;
  FilterInputStream fil_stm = null;
  int count = 0;

  try {
   // Instantiates FileInputStream and 
   // FilterInputStream
   fis_stm = new FileInputStream("D:\\includehelp.txt");
   fil_stm = new BufferedInputStream(fis_stm);

   // Loop to read until available
   // bytes left
   while ((count = fil_stm.read()) != -1) {

    // By using available() method is to
    // return the available bytes to be read
    int avail_bytes = fil_stm.available();

    // Display corresponding bytes value
    byte b = (byte) count;

    // Display value of avail_bytes and b
    System.out.print("fil_stm.available(): " + avail_bytes);
    System.out.println(" : " + "byte: " + b);
   }
  } catch (Exception ex) {
   System.out.println(ex.toString());
  } finally {
   // with the help of this block is to
   // free all necessary resources linked
   // with the stream
   if (fis_stm != null) {
    fis_stm.close();

    if (fil_stm != null) {
     fil_stm.close();
    }
   }
  }
 }
}

Output

fil_stm.available(): 15 : byte: 74
fil_stm.available(): 14 : byte: 97
fil_stm.available(): 13 : byte: 118
fil_stm.available(): 12 : byte: 97
fil_stm.available(): 11 : byte: 32
fil_stm.available(): 10 : byte: 87
fil_stm.available(): 9 : byte: 111
fil_stm.available(): 8 : byte: 114
fil_stm.available(): 7 : byte: 108
fil_stm.available(): 6 : byte: 100
fil_stm.available(): 5 : byte: 46
fil_stm.available(): 4 : byte: 46
fil_stm.available(): 3 : byte: 46
fil_stm.available(): 2 : byte: 46
fil_stm.available(): 1 : byte: 46
fil_stm.available(): 0 : byte: 46


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.