• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reload a File when changes happen in that file  RSS feed

 
Deva Devan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How to reload a file when changes happen in that file ?

How can we implement with FileAlterationObserver for this reload process?

Thanks,

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an interesting question. My first idea would be to set up a thread polling files (or a list of files) for changes, and firing events when such a change was detected. Then you could observe that. (Be sure to put in some sleep time though, so that thread doesn't eat up all of your CPU.)

Another approach would be to use the java.nio.file.WatchService, which will use the native OS services if possible, and so should be able to avoid polling. The downside is that it doesn't exist until Java 7.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another approach would be to use the java.nio.file.WatchService, which will use the native OS services if possible, and so should be able to avoid polling. The downside is that it doesn't exist until Java 7.

Yes, I'd call its current non-existence a downside, too, especially if you want to use it
Kinda strange that the Java Tutorial makes you believe it exists already: http://download.oracle.com/javase/tutorial/essential/io/notification.html

Apache Commons VFS has the org.apache.commons.vfs.impl.DefaultFileMonitor class which does exactly this.

I think the more interesting part of the question is how to make the program acknowledge the changes in the file. This might be very simple -maybe it's sufficient to read values into an existing data structure-, but it could also be quite complex, like if the file contains Java classes that should be used instead of previously defined ones.
 
Martijn van der Geest
Greenhorn
Posts: 5
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make an MD5 checksum of the file and then every x seconds make another checksum and compare to the previous one. If the checksum changed, the file changed.
And yes, java 7 will have a better solution for this problem.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use a checksum? If you're going to monitor and store some old value to compare against, why not use the last modification date? That's available from Java.
 
Martijn van der Geest
Greenhorn
Posts: 5
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That method works almost everytime and i guess it's better performance wise.
However the updating of the modification dates can have a delay. There even seems to be ways to modify a file without changing the modification date.
If you want to be 100% sure if a file has changed use my method. if 99.999% suits you use Rob's.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!