java.io.BufferedInputStream.read(byte[] b, int off, int len) 方法讀取位元組的輸入流len個位元組到位元組陣列,開始在一個給定的偏移量。這種方法反復呼叫底層流的read()方法。
該疊代讀繼續進行,直到下列條件之一為真:
以下是java.io.BufferedInputStream.read(byte[] b, int off, int len)方法的宣告
public int read(byte[] b, int off, int len)
b - 位元組陣列進行填充。
off - 從開始的偏移儲存數。
len - 要讀取的位元組數。
IOException -- 如果發生I/O錯誤。
下面的例子顯示java.io.BufferedInputStream.read(byte[] b, int off, int len)方法的用法。
package com.yiibai; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; public class BufferedInputStreamDemo { public static void main(String[] args) throws Exception { InputStream inStream = null; BufferedInputStream bis = null; try{ // open input stream test.txt for reading purpose. inStream = new FileInputStream("c:/test.txt"); // input stream is converted to buffered input stream 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 bis.read(buf, 2, 3); // for each byte in buf for (byte b : buf) { System.out.println((char)b+": " + b); } }catch(Exception e){ e.printStackTrace(); }finally{ // releases any system resources associated with the stream if(inStream!=null) inStream.close(); if(bis!=null) bis.close(); } } }
假設有一個文字檔案 c:/ test.txt,它具有以下內容。該檔案將被用作輸入在範例程式:
ABCDE
讓我們來編譯和執行上面的程式,這將產生以下結果:
: 0 : 0 A: 65 B: 66 C: 67