Question

I want to seek in a file which is located on SDCARD (External Storage).
I opened it in read-only mode.

in = new RandomAccessFile(filePath, "r");

Everything for reading from this file stream is ok until i want to seek in file.

long seekPosition = 100;//sample value
in.seek(seekPosition);

the seek position of course is not bigger than file size and not negative. however i face an exception whith no details. here is exception trace:

java.io.IOException
at java.io.RandomAccessFile.openCheck(RandomAccessFile.java:258)
at java.io.RandomAccessFile.seek(RandomAccessFile.java:648)
....

The question is why this happens and how can i fix it?
Is it related to seek in file which is located on shared storage or i missed something in this process?
My main concern is to access file bytes in random access method.

Was it helpful?

Solution

Well the (private) RandomAccessFile.openCheck() method (in Harvest / Android) simply checks that the file is currently open. If it throws an IOException, that means you've closed the RandomAccessFile object.

Lesson: If you get an exception that you don't expect / don't understand:

  1. Read the exception message1.
  2. Look at the stack trace to see where the exception is thrown.
  3. Find the source code. (Google usually works for me ...)
  4. Look at the source code to see why it is thrown.

1 - It doesn't help in this case, because for some bizarre reason they throw IOException with no message. Its a BUG in my opinion.

Reference:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top