在某个文件夹中选择粘贴选项(复制文件时)时如何生成事件,其中该事件应获取选择粘贴选项的文件夹的路径。
最佳答案
此代码监视目录中添加、删除或修改的文件:
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 的动机。
关于java - 将文件粘贴到文件夹中时获取文件夹的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15886167/