調用FileInputStream中的read()方法時,read()從輸入流中讀取的字節是按順序讀取的,並且只讀一遍,比如下面的示例代碼中,"test.txt"文件裏有"abcdef",每次讀取3個字符,則第一次讀取的爲abc,第二次讀取的爲def。
示例代碼:
FileInputStream fis=new FileInputStream("test.txt");
byte[] b=new byte[10];
while(true){
fis.read(b,0,3);
}
至於爲什麼順序讀取,看它繼承的父類inputStream中的方法read()說明中有寫道。
public abstract int read()
throws IOException
Reads the next byte of data from the input stream. The value byte is returned as an int
in the range 0
to 255
. If no byte is available because the end of the stream has been reached, the value -1
is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.