Java FileInputStream.read(byte[] b, int off, int len)方法範例
FileInputStream
的FileInputStream.read(byte[] b, int off, int len)
方法具有以下語法。
public int read(byte[] b, int off, int len) throws IOException
在下面的程式碼顯示如何使用FileInputStream.read(byte[] b, int off, int len)
方法。
import java.io.IOException;
import java.io.FileInputStream;
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, 2, 1);
System.out.println("Number of bytes read: " + i);
// for each byte in buffer // Copyright: W w w .y i I bA i.c o M
for (byte b : bs) {
// converts byte to character
char c = (char) b;
if (b == 0){
c = '-';
}
System.out.println(c);
}
}
}