I have an application that writes information to a file.This information is used post-execution to determine pass/failure/correctness of the application. I'd like to be able to read the file while it is being written so that I can do these pass/failure/correctness checks in real time. I assume it is possible to do this, but what are the gotcha's involved when using Java? If the reading catches up to the writing, will it just wait for more writes up until the file is closed, or will the read throw an exception at this point? If the latter, what do I do then?
Asked
Active
Viewed 47 times
1
-
Make use of the class `RandomAccessFile`? – dan1st Apr 19 '20 at 14:46
-
1Well, it's not a trivial task. You'll need to use a `java.nio.file.WatchService` and you'll need to find a way to know the file will not be updated anymore as you'll be watching for change events in the file rather than the end of file. – RealSkeptic Apr 19 '20 at 15:00
-
This [question](https://stackoverflow.com/questions/5886202/java-read-a-logfile-live) might help you too – fuggerjaki61 Apr 19 '20 at 16:08
-
1Yeah this is possible. but far from easy (in Java or other languages). It will likely be easier to revisit your requirements, for instance have the application trigger the checking itself. A critical gotcha will be synchronization (writes catching up to reads, as you say), which will pretty much have to be manually managed by your programs. – dimo414 Apr 19 '20 at 21:03