简体   繁体   English

使用RandomAccessFile检查空位置

[英]Checking null position using RandomAccessFile

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. 问题是,当位置实际未写入时,我得到(如预期的那样)EOFException。 I have been reading RandomAccessFile documentation to try to solve this problem, and tried researching online. 我一直在阅读RandomAccessFile文档以尝试解决此问题,并尝试在线研究。

Things I've tried: 我尝试过的事情:

  • Using a try-catch block and catching every time there is a EOFException (Using try-catch as a conditional statement). 使用try-catch块并在每次出现EOFException时进行捕获(将try-catch用作条件语句)。 It works, but it is horrible practice, and it is very inefficient, as for my case it is EOF the majority of the time. 它有效,但是这是可怕的做法,并且效率很低,就我而言,大多数情况下都是EOF。

  • Using a BufferReader to loop through and check the position. 使用BufferReader遍历并检查位置。 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. 我知道必须有一种直接的方法,我似乎无法找到正确的解决方案。

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. 您需要做的是使用一个线程,该线程仅使用FileChannel.size()读取那里的数据以检查更多数据。 This data is passed to a piped input. 该数据被传递到管道输入。 This allows you to have a second thread which reads the piped stream continuously eg using BufferedReader and blocks where more data is needed. 这使您可以拥有第二个线程,该线程可以连续读取管道流,例如使用BufferedReader并在需要更多数据的位置进行阻塞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM