This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I just want to know what's the easiest way to do this? Let's have I have an XML file which is constantly being updated and the application needs to detect if this file is updated and transfer the updated contents of the file to a cache. My idea of doing this is running a thread which periodically checks the last update time of the file and if there is a change, then it will load the contents of the file into the cache, to update the cache. Is there a better and more efficient way of doing this? If my idea is the best idea, do you guys think spawning thread could handle this or should I stick to the more complex timing APIs such as Quartz?
Ulf: You'll need to think about how to avoid reading the file while it's being updated.
Interesting point Ulf. Is there a way to assert whether a file is being updated? (Of course by some other program/application out of our application) This gets complex because i may have opened the file in a notepad/editor and keep saving periodically. Will it be correct to not read it for the entire period it is opened in an editor? Is the write to a file guaranteed to be atomic by the OS? or i may get a half saved file while reading?
Vilmantas: One easy solution is to use "quiet" period before loading the file. You require that the file timestamp is at least X seconds old before loading the file.
What will we get by doing this? Are you saying that if the timestamp is not X seconds older, the file may still be in the process of being written? I thought timestamp will be the last thing that must be updated when updating a file. (Although this will depend on the file system implementation which i am not a guru of! but still that makes more sense to me)