Search code examples
javawatchservice

WatchService API events, change editable files (word, odt,....)


I am using the following code to motorize a folder, to get the events of creation, modification, and elimination of files

public static void main(String[] args){

    try {
        Path dir = Paths.get("D:/Temp/");

        WatchService watcher = FileSystems.getDefault().newWatchService();

        dir.register(watcher,  StandardWatchEventKinds.ENTRY_CREATE, 
                StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); 

        WatchKey key;

        while ((key = watcher.take())!=null){


            for (WatchEvent<?> event : key.pollEvents()) {

                WatchEvent.Kind<?> kind = event.kind();

                @SuppressWarnings("unchecked")
                WatchEvent<Path> ev = (WatchEvent<Path>) event;
                Path fileName = ev.context();

                if(kind==StandardWatchEventKinds.ENTRY_CREATE){

                    System.out.println("New File Added, file Name " + fileName);
                }

                if(kind==StandardWatchEventKinds.ENTRY_DELETE){

                    System.out.println("File Deleted " + fileName);
                }

                if (kind == StandardWatchEventKinds.ENTRY_MODIFY ){

                    System.out.println("File Modified " + fileName);
                }
            }

            boolean valid = key.reset();
            if (!valid) {
                break;
            }
        }

    } catch (IOException ex) {
        System.err.println(ex);
    }
}

}

When I edit a word file (TEST.docx) within the folder that is being monitored and saved with changes, the following event result is displayed:

New File Added, file Name ~$TEST.docx
File Modified ~$TEST.docx
New File Added, file Name ~WRD0000.tmp
File Modified ~WRD0000.tmp
File Deleted TEST.docx
New File Added, file Name ~WRL0001.tmp
File Deleted ~WRD0000.tmp
New File Added, file Name TEST.docx
File Modified TEST.docx
File Modified ~WRL0001.tmp
New File Added, file Name ~WRD0002.tmp
File Modified ~WRD0002.tmp
File Deleted TEST.docx
New File Added, file Name ~WRL0003.tmp
File Deleted ~WRD0002.tmp
New File Added, file Name TEST.docx
File Modified TEST.docx
File Modified ~WRL0003.tmp
File Deleted ~WRL0003.tmp
File Deleted ~WRL0001.tmp
File Deleted ~$TEST.docx

Some events are caused by temporary files that are created by the Word application during this editing process.

Is there any way to filter the events to only get those from the word file (TEST.docx) and ignore the events originating from temporary files?

Thanks


Solution

  • In my application I filter them out simply by adding a if-condition:

    ...
    final Path changed = (Path) event.context();
    WatchEvent.Kind<?> kind = event.kind();
    if (changed.toString().startsWith("TEST.docx")) {
                        if(kind==StandardWatchEventKinds.ENTRY_CREATE){
    
                    System.out.println("New File Added, file Name " + fileName);
                }
    
    }
    

    As far as I know WatchService monitors all registered events (in your case ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE) in all files in the folder. The other way is forking the WatchService source but I don't see what what benefits that solution would have compared to this simple solution.