使用Java IO函数读取文件时出现神秘的EOF异常

时间:2022-07-15 20:25:38

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) >

>在myFile中寻找38128时出错,文件长度:java.io.RandomAccessFile.readLong(RandomAccessFile.java:758)中的java.io.RandomAccessFile.readInt(RandomAccessFile.java:725)中的java.io.EOFException为85742>

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.

但如果你看到我试图寻找'38128',其中文件长度是'85742'。它报告了EOF异常。我想知道它有可能吗?另一个进程定期向该文件追加内容并关闭文件处理程序。它使用DataOutputStream附加内容。我的过程是试图寻找一些地方并阅读它。还有一件事是我只有一次这个例外。我试图重现它,但它再也没有发生过。该文件仅在本地磁盘中。没有档案管理员。

Thanks D. L. Kumar

谢谢D. L. Kumar

2 个解决方案

#1


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()?

您是否在执行seek()的过程中确定文件的长度?其他修改处理是否已完成flush()?

#2


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.

可能已经告知写入数据的过程写入数据,但是可以缓冲数据以进行写入。在尝试读取数据之前,请务必在输出流上调用flush()。

#1


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()?

您是否在执行seek()的过程中确定文件的长度?其他修改处理是否已完成flush()?

#2


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.

可能已经告知写入数据的过程写入数据,但是可以缓冲数据以进行写入。在尝试读取数据之前,请务必在输出流上调用flush()。