1

我有一个简单的文件监视器设置来监视一个文件,因此当该文件的内容更改、添加或删除时我会收到通知。但是当文件被删除时,当它被添加回来时,我永远不会收到通知。这是我的代码片段:

String properyPath = "/some/directory/somexml.xml";
FileSystemManager fsManager;
fsManager = VFS.getManager();
FileObject listendir = fsManager.resolveFile( propertyPath );
DefaultFileMonitor fm = new DefaultFileMonitor( this );
fm.setRecursive( true );
fm.addFile( listendir );
fm.start();

当 propertyPath 文件被删除时,我会在我的 fileDeleted 实现中收到通知,但是当我再次重新创建文件时不会调用 fileAdded 方法。这是正常的吗?如果是这样,我如何设置它以在删除后收到添加通知?

4

3 回答 3

1

看起来你受到了这个问题的影响。如票中所述,您可以尝试设置零延迟:

fm.setDelay(0); 

或尝试修补的 DefaultFileMonitor。但是,如果您要同时查看太多文件,那么小的延迟可能会对性能产生影响。

于 2013-11-18T20:43:56.467 回答
0

Thanks Jk1 for pointing me to the issue. The answer is found here.

In summary, the FileMonitorAgent class in vfs removes the listener when a file gets deleted. (see the check method) below is the important block:

// If the file existed and now doesn't
if (this.exists && !this.file.exists())
{
  this.exists = this.file.exists();
  this.timestamp = -1;

  // Fire delete event

  ((AbstractFileSystem)
     this.file.getFileSystem()).fireFileDeleted(this.file);

  // Remove listener in case file is re-created. Don't want to fire twice.
  if (this.fm.getFileListener() != null)
  {
     this.file.getFileSystem().removeListener(this.file,
        this.fm.getFileListener());
  }

  // Remove from map
  this.fm.queueRemoveFile(this.file);
}

The link supplies a solution that has been submitted to vfs. I think the only solution at this point is to spawn a thread that re-adds the file to vfs after a few seconds. You have to sleep a few seconds because you get notified (via fireFileDeleted) and then the vfs code clears the listener, so you can re-add the listener until after you get notified and the vfs code clears the existing listener.

于 2013-11-19T16:45:14.270 回答
0

我最近也遇到了同样的问题。

我为解决它所做的是:

@Override
public void fileDeleted(FileChangeEvent arg0) throws Exception {
    if (arg0.getFile().getName().getBaseName().equals("ErrLog.txt")) {
        File removedFile = new File(arg0.getFile().getName().getPath());
        removedFile.getParentFile().mkdirs();
    }
}

就我而言,我正在监视 ErrLog.txt 的父目录。该目录已被删除。

于 2018-02-27T14:33:20.003 回答