0

I need to check if a position in a random access file has not been written to. The problem with this is, when the position actually hasn't been written to, I get (as anticipated) an EOFException. I have been reading RandomAccessFile documentation to try to solve this problem, and tried researching online.

Things I've tried:

  • Using a try-catch block and catching every time there is a EOFException (Using try-catch as a conditional statement). It works, but it is horrible practice, and it is very inefficient, as for my case it is EOF the majority of the time.

  • Using a BufferReader to loop through and check the position. I ended up running into many problems and decided that there must be a better way.

I don't want to do any copying one file over to another or any other work around. I know there has to be a direct way of doing this, I just can't seem to find the correct solution.

Dillon Burton
  • 363
  • 6
  • 16
  • The position must be a number which cannot be `null` can you show us some code which gives you a "null"? – Peter Lawrey Sep 19 '13 at 07:49
  • When I seek to a position that contains nothing and I try to read the position, I get an EOFException. Sorry, I should probably re-word my question. – Dillon Burton Sep 19 '13 at 07:51
  • So I would use the FileChannel.length() to check the data you want exists before you seek it. You don't need to keep checking it if you assume it doesn't get any smaller. – Peter Lawrey Sep 19 '13 at 07:53

1 Answers1

0

Are you trying to write a "tailer"?

What you need to do is have one thread which reads only the data which is there using FileChannel.size() to check for more data. This data is passed to a piped input. This allows you to have a second thread which reads the piped stream continuously e.g. using BufferedReader and blocks where more data is needed.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130