Java IO Tutorial - Java ByteArrayInputStream.read()








Syntax

ByteArrayInputStream.read() has the following syntax.

public int read()

Example

In the following code shows how to use ByteArrayInputStream.read() method.

//from w ww. j a  v a2 s . com

import java.io.ByteArrayInputStream;
import java.io.IOException;

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

    byte[] buf = { 65, 66, 67, 68, 69 };

    ByteArrayInputStream bais = new ByteArrayInputStream(buf);

    int b = 0;
    while ((b = bais.read()) != -1) {
      char c = (char) b;
      System.out.println("byte :" + b + "; char : " + c);
    }
    System.out.print(bais.read() + " Reached the end");
  }
}

The code above generates the following result.