Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JAVA watchservice api中,如何以所有驱动器为目标_Java_Watchservice - Fatal编程技术网

在JAVA watchservice api中,如何以所有驱动器为目标

在JAVA watchservice api中,如何以所有驱动器为目标,java,watchservice,Java,Watchservice,我可以让watch服务在JAVA中工作,但我想让它在所有驱动器和文件夹中工作,而不是只针对一个驱动器/文件夹 import java.io.IOException; import java.nio.file.*; /** * * @author Admin */ public class NewClass2 { public static void main(String[] args) throws IOException { WatchService watch

我可以让watch服务在JAVA中工作,但我想让它在所有驱动器和文件夹中工作,而不是只针对一个驱动器/文件夹

import java.io.IOException;
import java.nio.file.*;

/**
 *
 * @author Admin
 */
public class NewClass2 {
    public static void main(String[] args) throws IOException {
       WatchService watchService = FileSystems.getDefault().newWatchService();

        Path directory = Paths.get("C:\\");

        WatchKey watchKey = directory.register(watchService, 
                StandardWatchEventKinds.ENTRY_CREATE, 
                StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY);

        while (true) {
            for (WatchEvent<?> event : watchKey.pollEvents()) {
                System.out.println(event.kind());
                Path file = directory.resolve((Path) event.context());
                System.out.println(file + " was last modified at " + file.toFile().lastModified());
            }
        }
    }
    }
import java.io.IOException;
导入java.nio.file.*;
/**
*
*@author-Admin
*/
公共类NewClass2{
公共静态void main(字符串[]args)引发IOException{
WatchService WatchService=FileSystems.getDefault().newWatchService();
Path directory=Path.get(“C:\\”;
WatchKey WatchKey=directory.register(watchService,
StandardWatchEventTypes.ENTRY\u创建,
StandardWatchEventTypes.ENTRY\u删除,
StandardWatchEventTypes.ENTRY\u MODIFY);
while(true){
for(WatchEvent事件:watchKey.pollEvents()){
System.out.println(event.kind());
路径文件=directory.resolve((Path)event.context());
System.out.println(文件+)最后一次修改是在“+file.toFile().lastModified());
}
}
}
}

这听起来是一个非常宽泛的问题。你想在一台电脑上观看所有的驱动器,程序应该能够拾取任何连接的驱动器,这听起来像你需要一个非常Windows(或Linux,或,…)特定的API。然后你想查看所有驱动器上的所有文件夹?看这么多文件夹有点过分。无论我是否正确地理解了这一点,我认为你需要更具体地回答你的问题。监视系统上每个目录的目的是什么?我不需要查看每个文件夹,但至少需要查看所有驱动器