检查 Java 中所有文件句柄何时被释放

发布于 2024-12-23 03:34:24 字数 227 浏览 2 评论 0原文

我想监视一个目录,当文件出现时打开它,处理它,然后将其移动到另一个目录。问题是如何检查其他程序是否已完成编写。在 Java 7 中,我可以使用 FileSystem 中的 WatchService,但我只能检查文件的创建时间。我想知道什么时候所有文件句柄都被释放。

我的第一个想法是我可以获得独占锁,但事实证明,可以在另一个应用程序实际更新文件时将其踢出。

在 Java 中执行此操作的首选方法是什么?谢谢!

I want to monitor a directory, and when a file appears there open it, process it and then move it to another directory. The problem is how to check that the other program is done writing it. With Java 7, I can use a WatchService from FileSystem, but I can only check when the files are created. What I want is to know when all file handles are released.

My first thought was that I could obtain an exclusive lock, but it turned out that it was possible to kick out another application while it was actually updating the file.

What is the preferred way to do this in Java? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无需解释 2024-12-30 03:34:24

Watcher API 目前允许您查看在监视目录中创建、修改或删除文件系统对象时的事件。他们不会告诉您其他 inotify 事件(在 Linux 上)。事实上,我认为没有办法在纯Java中做到这一点。


几周前,我自己在寻找一种方法来执行此操作,我遇到了一个邮件线程,建议您编写 FileSystem api 的自定义实现,该实现提供支持其他文件的文件监视程序系统事件。我决定不再追求它,因为我有一个替代解决方案......基于我正在观看的文件是如何生成的知识。

就我而言,这些文件是由将图像文件保存到共享驱动器的仪器生成的。解决方案是监视新创建的文件的“修改”事件流。当它停止并且几秒钟(“稳定时间”)内不再有任何消息时,就可以处理文件了。

如果该解决方案被证明不可靠,则后备方案是直接使用 inotify 调用在 C / C++ 中实现监视和初始处理(拍摄文件快照)。这将使我能够直接观察文件关闭事件。

The Watcher APIs currently allow you to see events when a file system object is created, modified or deleted in a watched directory. They don't tell you about other inotify events (on Linux). In fact, I don't think there is a way to do this in pure Java.


I was looking for a way to do this myself a few weeks ago and I came across a mail thread that suggested that you could write a custom implementation of the FileSystem api that provided a file watcher that supported other file system events. I decided not to pursue it because I had an alternative solution ... based on knowledge of how the files I am watching are being produced.

In my case, the files are produced by instruments that save image files to a shared drive. The solution is to watch the stream of "modified" events for a newly created file. When it stops and no more have been forthcoming for a couple of seconds (the "settling time"), then the file can be processed.

If this solution proves to be unreliable, the fallback is to implement the watching and initial processing (taking a snapshot of the file) in C / C++ using the inotify calls directly. This will allow me to directly observe the file close event.

后来的我们 2024-12-30 03:34:24

基于文件的接口最简单的方法是:

  1. 发送者用更改的文件名写入文件(例如“example.xml_”)
  2. 当发送者完成写入文件时,他将其重命名(例如“example.xml_”为“example.xml”) ")
  3. 接收器仅扫描“*.xml”

The simplest way for a filebased interface is:

  1. The sender writes the files with a changed filename (e.g. "example.xml_")
  2. When the sender has finished writing the file, he renames it (e.g. "example.xml_" to "example.xml")
  3. The receiver scans only for "*.xml"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文