首页 > 解决方案 > 监视服务两次更新单个文件

问题描述

我第一次使用手表服务,我正在尝试使用手表服务更新文件,但它会两次更新该文件。

        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);

有时,当我想在删除第一个文件后再次创建相同的文件时,我也会遇到另一个错误。然后我得到以下错误

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

标签: javawatchservice

解决方案


推荐阅读