【问题标题】:in java,how to watch multiple directories在java中,如何查看多个目录
【发布时间】:2011-08-30 03:40:46
【问题描述】:

我想监控多个文件夹是否在文件夹中添加了新文件。 如果文件被添加到文件夹中,我想得到他的文件名。 如何做到这一点。

【问题讨论】:

标签: java watch


【解决方案1】:

请试试这个,

for(;;){

    System.out.println("START MONITORING  **************");


    Path faxFolder = Paths.get("E:\\activiti\\monitor\\m1");
    Path faxFolder2 = Paths.get("E:\\activiti\\monitor\\m2");
    WatchService watchService = FileSystems.getDefault().newWatchService();
    faxFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
    faxFolder2.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);


    boolean valid = true;
    WatchKey watchKey = watchService.take();
    for (WatchEvent<?> event : watchKey.pollEvents()) {
        WatchEvent.Kind kind = event.kind();
        if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
            String fileName = event.context().toString();
            System.out.println(fileName);

        }
    }



}

【讨论】:

    【解决方案2】:

    apache commons IO 库中有一个名为 File Monitor 的组件。我认为这正是您想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-29
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多