我有一个RandomAccessFile raFile我正在以固定大小的块将数据读入缓冲区的方法:
byte[] fileBuffer = new byte[BUFFER_SIZE];
while((readBytes = raFile.read(fileBuffer) >= 0) {
String bufferStr = new String(fileBuffer, 0, readBytes);
String testerStr = new String(fileBuffer);
System.out.println(readBytes+","+bufferStr.length()+","+testerStr.length());
}
我所期望的是raFile.read()读取与 一样多的字节BUFFER_SIZE(文件末尾除外)并将相同的值复制到readBytes. 虽然这在很大程度上是正确的,但偶尔,我会得到以下BUFFER_SIZE4096 的输出:
readBytes bufferStr testerStr
4096 4092 4092
4096 4090 4090
4096 4094 4094
4096 4095 4095
如果正在读取 4096 字节,为什么即使不在文件末尾,长度bufferStr也testerStr小于该值?
参考:这表示read()返回读入缓冲区的字节总数。