Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.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/WatchEvent/来自事件的路径_Java_Java 7 - Fatal编程技术网

Java WatchService/WatchEvent/来自事件的路径

Java WatchService/WatchEvent/来自事件的路径,java,java-7,Java,Java 7,我递归地监视一个目录(以及所有子目录和文件)的变化 似乎,如果我在根目录的子目录中创建或删除目录或文件以监视包含在WatchEvent实例中的路径(通过context())时没有父目录,因此rootDirToWatch.resolve(event.context())不会返回我想要的路径 例如: 监视/home/johannes/test,然后我在/home/johannes/test/foo/bar中创建一个名为baz的新目录,我得到一个新路径实例 /home/johannes/test/ba

我递归地监视一个目录(以及所有子目录和文件)的变化

似乎,如果我在根目录的子目录中创建或删除目录或文件以监视包含在WatchEvent实例中的路径(通过context())时没有父目录,因此
rootDirToWatch.resolve(event.context())
不会返回我想要的路径

例如:

监视
/home/johannes/test
,然后我在
/home/johannes/test/foo/bar
中创建一个名为
baz
的新目录,我得到一个新路径实例
/home/johannes/test/baz
而不是
/home/johannes/test/foo/bar/baz

有什么建议吗?出了什么问题

我只是使用一个访问者来监视某个根目录中的所有子目录(监视整个目录及其所有子目录):

编辑:我想我真的必须使用一个访问者,或者至少向观察者注册所有子目录。由于WatchEvent返回一个相对路径,因此很清楚它为何会按照所述的方式运行,但我不想再次遍历该目录,以找到从要监视的根目录到层次结构中某个地方添加/删除/修改的文件的路径

编辑:我找到了解决方案(“索引”键):

watchable()方法将返回原始watchable,以便您可以将其用作父目录。

Alan是正确的

您应该使用钥匙中的可观察按钮

这是我应用程序中的Scala代码。我想你会读到的

object FsNotifType extends Enumeration {
  val Create, Update, Delete = Value
}

case class FsNotif(notifType: FsNotifType.Value,path: Path)

object FsNotif {
  def apply(watchKey: WatchKey,event: java.nio.file.WatchEvent[_]): FsNotif = {
    val fsNotifType = event.kind() match {
      case StandardWatchEventKinds.ENTRY_CREATE => FsNotifType.Create
      case StandardWatchEventKinds.ENTRY_MODIFY => FsNotifType.Update
      case StandardWatchEventKinds.ENTRY_DELETE => FsNotifType.Delete
      case _ => throw new IllegalStateException("Unknown FS event kind: " + event)
    }
    val watchedPath = watchKey.watchable().asInstanceOf[Path]
    val relativePath = event.context().asInstanceOf[Path]
    val absolutePath = watchedPath.resolve(relativePath)
    FsNotif(fsNotifType,absolutePath)
  }
}
这很好,但要注意一些特殊情况:

/**
 * Returns the object for which this watch key was created. This method will
 * continue to return the object even after the key is cancelled.
 *
 * <p> As the {@code WatchService} is intended to map directly on to the
 * native file event notification facility (where available) then many of
 * details on how registered objects are watched is highly implementation
 * specific. When watching a directory for changes for example, and the
 * directory is moved or renamed in the file system, there is no guarantee
 * that the watch key will be cancelled and so the object returned by this
 * method may no longer be a valid path to the directory.
 *
 * @return the object for which this watch key was created
 */
Watchable watchable();
/**
*返回为其创建此监视键的对象。这种方法将
*即使在取消键后,仍继续返回对象。
*
*因为{@code WatchService}打算直接映射到
*本机文件事件通知功能(如果可用),然后
*有关如何监视已注册对象的详细信息在实现中非常重要
*具体的。例如,在查看目录的更改时
*目录在文件系统中被移动或重命名,这是没有保证的
*监视键将被取消,因此
*方法可能不再是目录的有效路径。
*
*@返回为其创建此监视键的对象
*/
可观察的;可观察的;

很抱歉,我不知道如何处理这个问题。

我发现了一个困难的方法,
WatchEvent.context()
总是返回一个相对于当前工作目录的
路径(即使您调用
toAbsolutionPath()
)。尽管文件上说这是相对的,但它似乎不像常识所规定的那样起作用。感谢您提及解决方案。索引键似乎是获得正确路径的唯一方法(例如,
originalPath.resolve(event.context())
/**
 * Returns the object for which this watch key was created. This method will
 * continue to return the object even after the key is cancelled.
 *
 * <p> As the {@code WatchService} is intended to map directly on to the
 * native file event notification facility (where available) then many of
 * details on how registered objects are watched is highly implementation
 * specific. When watching a directory for changes for example, and the
 * directory is moved or renamed in the file system, there is no guarantee
 * that the watch key will be cancelled and so the object returned by this
 * method may no longer be a valid path to the directory.
 *
 * @return the object for which this watch key was created
 */
Watchable watchable();