Java IO Tutorial - Java FileInputStream.read(byte[] b)








Syntax

FileInputStream.read(byte[] b) has the following syntax.

public int read(byte[] b)  throws IOException

Example

In the following code shows how to use FileInputStream.read(byte[] b) method.

/*from w  w w .j av  a2s . c o  m*/
import java.io.FileInputStream;
import java.io.IOException;

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

    byte[] bs = new byte[4];

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read bytes to the buffer
    int i = fis.read(bs);

    System.out.println("Number of bytes read: " + i);
    // for each byte in buffer
    for (byte b : bs) {
      // converts byte to character
      char c = (char) b;
      System.out.println(c);
    }

  }
}