调用FileInputStream中的read()方法时,read()从输入流中读取的字节是按顺序读取的,并且只读一遍,比如下面的示例代码中,“demo.txt"文件里有"abcdefgh”,每次读取4个字符,则第一次读取的为abcd,第二次读取的为efgh。
示例代码:
FileInputStream inStream=new FileInputStream("demo.txt"); byte[] b=new byte[4]; while(true){ fis.read(b); }原因,看它继承的父类inputStream中的方法read()说明中有写道。
/** * Reads the next byte of data from the input stream. The value byte is * returned as an <code>int</code> in the range <code>0</code> to * <code>255</code>. If no byte is available because the end of the stream * has been reached, the value <code>-1</code> is returned. This method * blocks until input data is available, the end of the stream is detected, * or an exception is thrown. * * <p> A subclass must provide an implementation of this method. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @exception IOException if an I/O error occurs. */