为什么WatchService会产生如此多的操作? [英] Why does WatchService generate so many operations?

查看:1833
本文介绍了为什么WatchService会产生如此多的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.io.*;
import java.nio.file.*;

public class Tmp {

    public static void main(String [] args) throws IOException {
        int count = 0;
        Path path = Paths.get("C:\\tmp\\");
        WatchService ws = null;
        try {
            ws = FileSystems.getDefault().newWatchService();
            path.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
                    StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        while(true) {
            WatchKey key = null;
            try {
                key = ws.take();
            } catch(InterruptedException ie) {
                ie.printStackTrace();
            }

            for(WatchEvent<?> event: key.pollEvents()) {
                switch(event.kind().name()) {
                    case "OVERFLOW":
                        System.out.println(++count + ": OVERFLOW");
                        break;
                    case "ENTRY_MODIFY":
                        System.out.println(++count + ": File " + event.context() + " is changed!");
                        break;
                    case "ENTRY_CREATE":
                        System.out.println(++count + ": File " + event.context() + " is created!");
                        break;
                    case "ENTRY_DELETE":
                        System.out.println(++count + ": File " + event.context() + " is deleted!");
                        break;
                    default:
                        System.out.println(++count + ": UNKNOWN EVENT!");
                }
            }

            key.reset();
        }    
    }
}

当我运行这个然后然后打开Notepad ++然后创建一个新的空文件并将其保存为 a.txt C:\ tmp \ 目录我得到了输出:

When I run this and then opened the Notepad++ and then created a new empty file and saved it as a.txt in the C:\tmp\ directory I got the output:

1: File a.txt is created!
2: File a.txt is deleted!
3: File a.txt is created!

为什么?看起来该文件已创建然后被删除,然后再次创建。为什么?

Why is that? It looks like the file was created and then deleted and then created again. Why?

当我在文件中放入一些文本并保存时输出为:

When I put some text in the file and saved it the output was:

4: File a.txt is changed!
5: File a.txt is changed!

为什么会改变两次?

推荐答案

Watch Service的Modify事件会生成两个事件。当我们修改现有文件时,文件系统首先用0字节创建它并触发修改事件然后在其上写入数据。然后它再次触发修改事件。这就是它显示两个修改事件的原因。所以我做了什么来解决这个问题,我只是用计数器来检查我的任务应该只在偶数上触发一​​次

Watch Service's Modify event generates two events. When we modify an already existing file, the file system first creates it with 0 bytes and fires a modify event and then writes data on it. Then it fires the modify event again. That's why It was showing two modify events. So What I have done to solve this problem, I just use counter to check my task should be triggered only once on even count

        Path path = null;
        int count = 0;

        try {
            path = Paths.get(new Utility().getConfDirPath());
            System.out.println("Path: " + path);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

        WatchService watchService = null;
        try {
            watchService = FileSystems.getDefault().newWatchService();
            path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.ENTRY_DELETE);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }


        while(true) {
            WatchKey key = null;
            try {
                key = watchService.take();
            } catch(InterruptedException ie) {
                ie.printStackTrace();
            }

            for(WatchEvent<?> event: key.pollEvents()) {
                switch(event.kind().name()) {
                    case "ENTRY_MODIFY":
                        System.out.println(++count + ": File " + event.context() + " is changed!");

                        if (count%2==0) {
                            doOnChange(); // do whatever you want
                        }
                        break;
                    case "ENTRY_DELETE":
                        System.out.println(++count + ": File " + event.context() + " is deleted!");
                        break;
                    default:
                        System.out.println(++count + ": UNKNOWN EVENT!");
                }
            }

            // reset the key
            boolean valid = key.reset();
            if (!valid) {
                System.out.println("Key has been unregistered");
            }

        }    

这篇关于为什么WatchService会产生如此多的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆