tags:

views:

373

answers:

2

Hi, I got following exception when I am trying to seek to some file.

> Error while seeking to 38128 in myFile, File length: 85742 java.io.EOFException at java.io.RandomAccessFile.readInt(RandomAccessFile.java:725) at java.io.RandomAccessFile.readLong(RandomAccessFile.java:758) >

But If you see I am trying to seek to '38128' where as file length is '85742'. It reported EOF exception. I wonder how it is possible? Another process appends contents to that file periodically and closes the file handler. It appends contents using DataOutputStream. My process is trying to seek to some locations and reading it. One more thing is I got this exception only once. I tried to reproduce it but it never happened again. The file is in local disk only. No filer.

Thanks D. L. Kumar

+2  A: 

I would be very careful when trying to do random access on a file that is concurrently being written to from another process. It might lead to all kinds of strange synchronisation problems, as you are experiencing right now.

Do you determine the length of the file from the same process as the one doing the seek()? Has the other modifying processing done a flush()?

Jeroen van Bergen
Updated the question again. I am determining the length of the file from same process with which I am doing seek(). As I updated just now other process appends the things and closes it. as close uses flush() yesh flush is happening.
+1  A: 

The process writing the data may have been told to write the data, but the data could be buffered to write. Be sure to call flush() on the output stream prior to attempting to read the data.

Pesto
But in that case why the file length is also wrong. Ofcourse we can say when I am trying to seek() file is getting written and when I checked the length file is dumped. But every time other process appends very small amount of data. So it might not have happened during flushing.
Where are you checking the function length? You should be doing it in the same thread as the seek().
Pesto