Java IO Tutorial - Java BufferedInputStream .read (byte[] b, int off, int len)

Syntax[] b, int off, int len) has the following syntax.

public int read(byte[] b, int off, int len)  throws IOException


In the following code shows how to use[] b, int off, int len) method.

/* w w w  .j a  v  a2s  . c o m*/


public class Main {
  public static void main(String[] args) throws Exception {
    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    // read number of bytes available
    int numByte = bis.available();

    // byte array declared
    byte[] buf = new byte[numByte];

    // read byte into buf , starts at offset 2, 3 bytes to read, 2, 3);

    // for each byte in buf
    for (byte b : buf) {
      System.out.println((char) b + ": " + b);