0

I am using watch service first time, I am trying to update a file using watch service but it's updating that file two times.

        boolean valid = true;
        do{
          WatchKey watchKey = watchService.take();
          for(WatchEvent<?> event : watchKey.pollEvents()){
          WatchEvent.Kind kind = event.kind();

                if(StandardWatchEventKinds.ENTRY_CREATE.equals(kind)){
                    String fileName = event.context().toString();
                }

                if(StandardWatchEventKinds.ENTRY_MODIFY.equals(kind)){
                    String fileName = event.context().toString();
                }

                if(StandardWatchEventKinds.ENTRY_DELETE.equals(kind)){
                    String fileName = event.context().toString();
                }
            }
            valid = watchKey.reset();
        }while(valid);

Somtime I also getting an another error, when i want to create same file again just after deleting first file. then i am getting below error

java.io.FileNotFoundException: D:\Watson\docs\FMO-OMS_Requirements.docx (The process cannot access the file because it is being used by another process)

  • Probably you get two events? For example one modify and one create – Veselin Davidov May 28 '18 at 08:26
  • No, when I am creating a new file it's getting two different events (create and modify) but when I am updating the file it's getting the same event (modify) two times? – Manish Verma May 28 '18 at 08:32
  • Possible duplicate of [Java 7 WatchService - Ignoring multiple occurrences of the same event](https://stackoverflow.com/questions/16777869/java-7-watchservice-ignoring-multiple-occurrences-of-the-same-event) – JEY May 28 '18 at 08:55
  • I changes my watch service code as below:- – Manish Verma May 30 '18 at 10:19
  • Bt what's happening now, When i am using it in dubug mode everything is working fine but when i am runing it in normal mode it only working for first event. – Manish Verma May 30 '18 at 10:21

0 Answers0