1

In my Java app, on Linux, I need to periodically read some text files that change often. (these text files are updated by a separate app). Do I need to be concerned about the rare case when attempting to read the file at the exact moment it is being updated? If so, how can I guarantee that my reads always return without failing? Does the OS handle this for me, or could I potentially read 1/2 a file?

thanks.

2 Answers 2

2

The OS can help you achieve consistent reads, but it requires that both apps are written with this in mind.

In a nutshell, you open the file in your java app with exclusive read/write permission - this ensures that no one else, including your other app is modifying the file while you are reading it. The FileLock class can help you ensure you have exclusive access to a file.

Your other app will then periodically try to write to the file. If it does this at the same time you are reading the file, then access will be denied, and the other app should retry. This is the critical part, since if the app doesn't expect the file to be unavailable and treats this as a fatal error condition, the write will fail, and app doesn't save the data and may fail/exit etc.

If the other app must always be able to write to the file, then you have to avoid using exclusive reads. Instead, you have to try to detect an inconsistent read, such as by checking the last modified timestamp when you start reading, and when you finish reading. If the timestamps are the same, then you are good to go and have a consistent read.

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate your quick and helpful response! For the second scenario, in which the other app always needs to be able to write to the file (actually overwrite each time), will I always be able to do at least an inconsistent read, or do I need to also check if the file is locked (and retry) for reading? I guess this depends on if the other app does an exclusive write? what if it is copied via ftp or a script that does a linux file copy?
0

Yes, you need to worry about this.

No, your reads shouldn't "fail" AFAIK, unless the file is momentarily being locked, in which you can catch the exception and try again after a brief pause. You might certainly, though, get more or less data than you expected.

(If you post code we might be able to comment more accurately on what'll happen.)

2 Comments

how can I know I didn't get less data - Read the file 2x? check the modification date? the files are comma delimited and there is no special marker or way to know that I made it to the end of the file
Well, if you're using something like FileInputStream.read() then it will return an int saying how many bytes were read, and if it doesn't match what you expected then there was an update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.