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.



你可能感兴趣的:(JAVA学习)