This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Does anyone know how to monitor a file for changes (content, attributes, etc)? Sure I can spin a thread to check for last modified date once in a while, but I don't want the overhead. Thanks in advance.
SCJP, SCJD<br />"He who knows others is wise; He who knows himself is enlightened."<br />-- Lao-Tzu (6th century B.C.), Legendary Chinese philosopher
Originally posted by Tongyu Wang: Does anyone know how to monitor a file for changes (content, attributes, etc)? Sure I can spin a thread to check for last modified date once in a while, but I don't want the overhead. Thanks in advance.
I think you answered your own question there, you're looking for something that "monitors" for changes and notifies some class or does something about it itself. This implies a thread, maybe using a Timer, and it doesn't necessarily have to be expensive. I may be wrong, haven't slept well enough
No. I meant some kind of notification object in the Java API I can "wait" on and receive notification when change happens. I know win32 has a FindFirstChangeNotification call to get such a handle but not sure if Java API exposes it.
I've never seen an Java version of this. It does seem like something which the standard libraries should provide, but as far as I know, they don't. I hate making a wait-and-recheck busy loop - seems like there should be an addFileChangeListener() method somewhere. If anyone else knows of one, I'd like to hear of it.
Tongyu Wang wrote:Does anyone know how to monitor a file for changes (content, attributes, etc)? Sure I can spin a thread to check for last modified date once in a while, but I don't want the overhead. Thanks in advance.
Usually, we don't like it if someone wakes the zombies, but I guess your answer will be useful if anyone is searching for this.
But do note that the watch service API is not yet available even in today's version of Java (Java 6); this is a new feature that's going to be available in Java 7, which will not be released before September 2010. So, unfortunatelty most people today will not yet be able to use this.