Java FileInputStream.read(byte[] b)方法範例

2019-10-16 22:14:53

Java FileInputStream.read(byte[] b)方法範例

FileInputStreamFileInputStream.read(byte[] b)方法具有以下語法。

public int read(byte[] b) throws IOException

範例

在下面的程式碼顯示如何使用FileInputStream.read(byte[] b)方法。


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); // At:  wW w . yi i b AI.C O  m 

    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);
    }

  }
}