1

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

4

2 回答 2

2

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

于 2011-05-21T08:50:47.640 回答
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);

        }
    }



}
于 2014-02-25T11:40:40.843 回答