在java中,如何查看多个目录

发布于 2024-11-09 08:43:48 字数 63 浏览 2 评论 0 原文

我想监视多个文件夹是否在文件夹中添加了新文件。 如果文件添加到文件夹中,我想获取文件的名称。 如何做到这一点。

i want to monitor the multiple folders whether new files are added in the folders.
if files are added in folder,i want to get he name of the file.
How to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉黛浅 2024-11-16 08:43:48

有一个名为 apache commons 中的 >文件监视器 IO 库。我想这正是您正在寻找的。

Theres a component called File Monitor in the apache commons IO library. I Think it's just what you're looking for.

我做我的改变 2024-11-16 08:43:48

请尝试这个,

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);

        }
    }



}

please try this ,

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);

        }
    }



}
燃情 2024-11-16 08:43:48

我比较了 Java WatchService 和 commons-io FileAlterationMonitor

正如本教程中已经写的那样,WatchService 不需要轮询。它可以阻塞线程直到事件可用。
另一方面,FileAlterationMonitor 使用线程进行轮询,并在需要时通知所有已注册的侦听器。

从上面可以得出结论,使用 WatchService 性能更高。然而,性能是特定于实现的。作为 文档说,对于不提供基于事件的通知的平台,可以实施轮询。因此,WatchService 的性能有可能更高,但随着我们转向所有支持事件的现代文件系统,这种机会就会越来越大。

WatchService 还有另一个警告:它会准确监视您注册的路径。没有递归操作。虽然您现在可以注册创建的所有子目录来监视它们,但 WatchService 发出的 WatchEvent 仅携带相对路径。这样您就不会知道您正在观看的几个目录中的哪一个真正产生了该事件。

commons-io 解决方案并非如此。由于它将更改作为 java.io.File 对象进行通信,因此每个事件都包含完整路径。更重要的是,一旦您查看目录,子文件夹中的更改也会被检测到,而无需额外的努力。

由此决定何时使用可能的内容:

  • 如果您有一个目录/浅层次结构来查找更改,请使用 WatchService。
  • 如果您有许多目录或深层层次结构需要监视,请使用 FileAlterationMonitor。

I compared the two different approaches of the Java WatchService and commons-io FileAlterationMonitor.

As it is written already in this tutorial, the WatchService does not require polling. It can block a thread until events are available.
On the other hand the FileAlterationMonitor uses a thread for polling and if needed notifies all registered listeners.

From the above one could conclude that using the WatchService is more performant. However the performance is implementation specific. As the documentation says for platforms that do not offer event based notification a polling may be implemented. So there is just a chance that WatchService is more performant, but as we move to modern filesystms which all support events the chances are growing.

There is another caveat with WatchService: it watches exactly the path you register for. There is no recursive operation. While you could now register all the subdirectories that get created to watch them too, the WatchEvent emitted by the WatchService only carries a relative path. With that you would not know which of the several directories you are watching actually produced the event.

Not so the commons-io solution. As that communicates the changes as java.io.File objects, each of the events contains the full path. Even more, once you watch a directory, also changes in subfolders are detected without additional effort.

With that the decision when to use what might be:

  • Use WatchService if you have one directory/a shallow hierarchy to look for changes.
  • Use FileAlterationMonitor if you have many directories or deep hierarchies to watch.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文