Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 监视服务将单个文件更新两次_Java_Watchservice - Fatal编程技术网

Java 监视服务将单个文件更新两次

Java 监视服务将单个文件更新两次,java,watchservice,Java,Watchservice,我第一次使用watch服务,我尝试使用watch服务更新一个文件,但它更新了该文件两次 boolean valid = true; do{ WatchKey watchKey = watchService.take(); for(WatchEvent<?> event : watchKey.pollEvents()){ WatchEvent.Kind kind = event.kind();

我第一次使用watch服务,我尝试使用watch服务更新一个文件,但它更新了该文件两次

        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);
boolean valid=true;
做{
WatchKey WatchKey=watchService.take();
for(WatchEvent事件:watchKey.pollEvents()){
WatchEvent.Kind-Kind=event.Kind();
if(StandardWatchEventTypes.ENTRY_CREATE.equals(种类)){
字符串文件名=event.context().toString();
}
if(StandardWatchEventTypes.ENTRY_MODIFY.equals(种类)){
字符串文件名=event.context().toString();
}
if(StandardWatchEventTypes.ENTRY_DELETE.equals(种类)){
字符串文件名=event.context().toString();
}
}
valid=watchKey.reset();
}有效期;
有时我还遇到另一个错误,当我想在删除第一个文件后再次创建同一个文件时。然后,我得到下面的错误


java.io.FileNotFoundException:D:\Watson\docs\FMO-OMS\u Requirements.docx(该进程无法访问该文件,因为它正被另一进程使用)

您可能会收到两个事件?例如,一个modify和一个createNo,当我创建一个新文件时,它会得到两个不同的事件(create和modify),但当我更新文件时,它会得到相同的事件(modify)两次?我可能会重复更改我的手表服务代码,如下:-Bt现在发生了什么,当我在dubug模式下使用它时,一切正常,但当我在正常模式下运行它时,它只对第一个事件有效。