使用WatchService识别.txt中的更改

时间:2015-07-20 19:18:16

标签: java text watchservice

我正在尝试使用WatchService扫描目录以查找对多个文件的任何更改。我能够识别给定目录中已更改的确切文本文件。我想知道是否有某种方法让我的程序然后准确显示更改是什么,然后搜索特定变量的更改?

新条目看起来像这样

07/02/15 11:44,88,55

我想拉这条线,然后扫描88或55是否达到100或更高。

这是我目前的代码。

public void scanChanges() {
    Path p = Paths.get("C:\\Users\\achamberlain\\Desktop\\Data");
    try {
        WatchService watcher = p.getFileSystem().newWatchService();
        p.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);

        for (;;) {
            WatchKey key = null;
            key= watcher.take();

            for (WatchEvent<?> event : key.pollEvents()) {
                String location;
                location = (p + "\\" + event.context().toString());
                Watcher sendlocation = new Watcher();
                sendlocation.readFile(location);
                key.reset();
            }
        }
    } catch (IOException | InterruptedException e) {
    }
}

public class Watcher {

    public static void main(String[] args) throws IOException {
        Modify modClass = new Modify();
        modClass.scanChanges();   
    }

    //changes "location" to "fileName" for use in this class
    public void readFile(String fileName){
        System.out.println(fileName);
    }
}

0 个答案:

没有答案