1

フォルダに新しいファイルが追加されていないか、複数のフォルダを監視したい。ファイルがフォルダに追加された場合、ファイルの名前を取得したい。これを行う方法。

4

2 に答える 2

2

ApacheCommonsIOライブラリにFileMonitor と呼ばれるコンポーネントがあります。まさにあなたが探しているものだと思います。

于 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 に答える