简体   繁体   中英

Java 7 WatchService — ENTRY_MODIFY triggered on file open

I have a program that watches a directory for file update using WatchService . I get events when I modify a file. However, I notice that even if I open a file in vi, and do not modify its contents, the watch service poll method is invoked. My code is as follows:

watcher = FileSystems.getDefault().newWatchService();
Path path = Paths.get("conf");
path.register(watcher, ENTRY_MODIFY);
WatchKey key = null;

key = watcher.poll(MAX_WAIT_TIME, TimeUnit.SECONDS);
if (key != null) {
for (WatchEvent<?> events : key.pollEvents()) {
        WatchEvent<Path> ev = cast(events);
        Path fileName = ev.context();
    }

In the above code, watcher.poll waits for MAX_WAIT_TIME for an ENTRY_MODIFY event. However, when I open a file within the directory being watched, and close without changing its contents... the watcher.poll receives an event and stops waiting. Is there some parameter that needs to be set and I am missing?

If you save the file before closing, the OS will see the file as modified even if there is no change to it and these will trigger the ENTRY_MODIFY event. Also your code is taking only one watch key. If you want continue watching the directory you need to put your watcher.poll instruction into a loop.

I tried the below code in java7 and it works fine for me in windows.Also please try the watchkey reset option.Below is the code used for WatchService:

                    import java.nio.file.FileSystems;
                    import java.nio.file.Path;
                    import java.nio.file.Paths;
                    import java.nio.file.WatchEvent;
                    import java.nio.file.WatchKey;
                    import java.nio.file.WatchService;
                    import java.nio.file.StandardWatchEventKinds;

                    public class WatchServiceJava7Feature {


                        public static void main(String[] args) throws Exception {
                            System.out.println("here ");
                            WatchService  watchService = FileSystems.getDefault().newWatchService();
                            Path path= Paths.get("C:\\User\\code\\watchservice\\");
                            System.out.println("here 1");
                            path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY);
                            while(true)
                            {
                            WatchKey key = watchService.take(); // this will return the keys
                            for(WatchEvent<?> event : key.pollEvents()) 
                             {
                                 WatchEvent<Path> watchEvent = (WatchEvent<Path>) event;
                                 WatchEvent.Kind<Path> kind = watchEvent.kind();
                                 switch(kind.name()) {
                                    case "ENTRY_MODIFY":
                                        System.out.println("Case Modify :Event on "+ event.context().toString() + " is "+ kind);
                                        break;
                                    case "ENTRY_DELETE":
                                        System.out.println("Case Delete :Event on "+ event.context().toString() + " is "+ kind);
                                        break; 
                                    case "ENTRY_CREATE":
                                        System.out.println("Case Create :Event on "+ event.context().toString() + " is "+ kind);
                                        break; 
                                }         

                             }
                             key.reset();  
                           }

                        }

                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM