JAVA學習日誌:FileInputStream的read()讀取爲順序讀取

調用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.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章