如何在某个文件夹中选择粘贴选项(复制文件时)时生成事件,其中该事件应该获取选择粘贴选项的文件夹的路径。
答案 0 :(得分:2)
此代码监视添加,删除或修改文件的目录:
Path testDirectory = Files.createTempDirectory( getClass().getName());
WatchService watcher = FileSystems.getDefault().newWatchService();
testDirectory.register( watcher,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE );
for(;;) {
WatchKey key = watcher.take();
log( "key = watcher.take()" );
if( key.isValid()) {
log( "key.isValid()" );
List< WatchEvent< ? >> lst = key.pollEvents();
for( WatchEvent<?> e : lst ) {
log( "WatchEvent polled: " + e.kind() + ": " + e.context());
if( e.kind() == StandardWatchEventKinds.ENTRY_CREATE ) {
Path path = (Path)e.context();
File file = path.toFile();
addFile( new File( testDirectory.toFile(), file.getPath()));
}
else if( e.kind() == StandardWatchEventKinds.ENTRY_DELETE ) {
Path path = (Path)e.context();
File file = path.toFile();
removeFile( new File( testDirectory.toFile(), file.getPath()));
}
}
key.reset();
}
}
此代码涉及的类是:
This part of the io tutorial显示更多内容并解释此API的动机。
答案 1 :(得分:0)