在java中查看目录和子目录以进行创建,修改和更改 [英] Watching a Directory and sub directory for create, modify and Changes in java

查看:160
本文介绍了在java中查看目录和子目录以进行创建,修改和更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在目录C:/ java / newfolder中更改了一些代码进行检测它运行良好。我在下面给出了。

I have make some code for detect changed in directory C:/java/newfolder it working good. i have given below.

import java.nio.file.*;
import java.util.List;

public class DirectoryWatchExample {
public static void testForDirectoryChange(Path myDir){

        try {
           WatchService watcher = myDir.getFileSystem().newWatchService();
           myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE,
           StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);

           WatchKey watckKey = watcher.take();

           List<WatchEvent<?>> events = watckKey.pollEvents();
           for (WatchEvent event : events) {
                if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
                    System.out.println("Created: " + event.context().toString());
                }
                if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
                    System.out.println("Delete: " + event.context().toString());
                }
                if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
                    System.out.println("Modify: " + event.context().toString());
                }
            }

        } catch (Exception e) {
            System.out.println("Error: " + e.toString());
        }
}

public static void main (String[] args) {
    Path myDir = Paths.get("c:/java/newfolder/");
    //define a folder root
    testForDirectoryChange(myDir);

}
} 

现在我只看目录。但我只需要查看所有子目录。

now i watching only the directory. But i need to watch only the all sub directory.

ex: c:/ java / newfolder / folder1,c:/ java / newfolder / folder2,c:/ java / newfolder / folder3 ...... etc

我给出了子目录上面的例子

i given examples above sub directory


c:/ java / newfolder /*..

c:/java/newfolder/*..

我需要观察所有子目录给我一些解决方案吗?

i need to watch the all sub directory give me some solutions ?

推荐答案

我不熟悉路径 API,所以请注意以下几点。

I'm not familiar with the Path API, so take the following with a grain of salt.

您正在注册一个目录进行监控,并将收到通知每当修改/创建/删除其中一个直接后代时。

You're registering one directory for monitoring, and will receive notifications whenever one of its direct descendants is modified / created / deleted.

您需要做的第一件事就是注册所有子目录以供观察:

The first thing you need to do is register all of its subdirectories for watching:

// Used to filter out non-directory files.
// This might need to filter '.' and '..' out, not sure whether they're returned.
public class DirectoryFilter implements FileFilter {
    public boolean accept(File file) {
        return file.isDirectory();
    }
}


// Add this at the end of your testForDirectoryChange method
for(File dir: myDir.toFile().listFiles(new DirectoryFilter())) {
    testForDirectoryChange(dir.toPath());
}

这将递归浏览您的文件结构并注册每个目录以供观看。
请注意,如果您的目录树太深,递归可能不是一个可接受的解决方案,您可能需要'iterify'它。

This will recursively explore your file structure and register every directory for watching. Note that if your directory tree is too deep, recursion might not be an acceptable solution and you might need to 'iterify' it.

第二件事你需要做的是,无论何时收到目录创建事件,都不要忘记注册新目录进行监控。

The second thing you need to do is, whenever you receive a directory creation event, not forget to register the new directory for monitoring.

无论如何我都是这样做的,但没有一个有效的Java 1.7安装,我无法测试它。请告诉我它是否有效!

That's how I'd do it anyway, but not having a valid Java 1.7 installation at hand, I can't test it. Do let me know if it works!

这篇关于在java中查看目录和子目录以进行创建,修改和更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆