Java IO Tutorial - Java BufferedInputStream .available ()








Syntax

BufferedInputStream.available() has the following syntax.

public int available()  throws IOException

Example

In the following code shows how to use BufferedInputStream.available() method.

//from w w w .  j  a v a  2  s . c  o m

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

public class Main {
  public static void main(String[] args) throws Exception {

    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    // read until a single byte is available
    while (bis.available() > 0) {
      // get the number of bytes available
      Integer nBytes = bis.available();
      System.out.println("Available bytes = " + nBytes);

      // read next available character
      char ch = (char) bis.read();

      // print the read character.
      System.out.println("The character read = " + ch);
    }

  }
}