Java FilterInputStream available() Example

FilterInputStream class available() method example. This example shows you how to use close() method.


syntax is : public int available() throws IOException
Method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

Here is the code.
/**
 * @(#) AvailableFilterInputStream.java
 * A class representing use of method available() of FilterInputStream 
 class in java.io Package.
 * @Version  09-June-2008
 @author   Rose India Team
 */
import java.io.*;

public class AvailableFilterInputStream extends FilterInputStream {

    AvailableFilterInputStream(FileInputStream objFin){
    super(objFin);
    }

  public static void main(String[] argsthrows IOException{

    byte[] byteArray = new byte[]{'M','A','H','E','N','D','R','A'};

    //Create a file with a specified name and file extension.
    File file = new File("mahendra.txt");

    // Create a output stream and write specified bytes in file.
        FileOutputStream objFileStream = new FileOutputStream(file);
    objFileStream.write(byteArray);

    // create a file input stream to read content of the file.
      FileInputStream objFin = new FileInputStream(file);
    ReadFilterInputStream obj =new ReadFilterInputStream(objFin);

        // print content of the file.
    System.out.print("Content in the file : ");
    for (int i=0; i<byteArray.length ;i++ ){
      System.out.print((char)byteArray[i]);
    }
        System.out.print("\n");
    // available() method call.
     int byteCount = obj.available();
     System.out.print("Total number of bytes that can be read : "+byteCount);
  }
}

Output of the program.
 Content in the file : MAHENDRA
Total number of bytes that can be read : 8

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US