Just guessing, but you probably have an offset
greater than 0
; if you're reading data.length
bytes starting from a position greater than 0, you'll pass the end of the data
array, which may be throwing the IndexOutOfBoundsException
.
So, if you want to read the full array, offset
variable should be set to 0
. Besides, if you don't want to start from 0, you should read data.length - offset
bytes.-
randFile.readFully(data, offset, data.length - offset);