Java - 如何在写入文件时检测文件删除?

发布于 2024-11-16 22:06:18 字数 556 浏览 3 评论 0原文

我们的java程序不断地将一些重要的数据写入文件中。现在,每当某个用户手动删除文件(java 程序正在写入的文件)时,我们希望抛出一些异常或在控制台上显示某种错误消息。

我发现java在尝试写入已删除的文件时不会抛出任何异常。

方法 I - 启动一个新线程,该线程将不断检查文件是否存在,并在文件被删除时通知主 java 程序。这在我们的例子中不起作用,因为我们将有几个文件供 java 程序写入,因此为每个文件启动一个新线程将不会提高内存效率。

方法 II - 每次在写入文件之前检查文件是否存在。这也不是一个好方法,因为检查文件是否存在是一项成本较高的操作。

方法 III - 扩展 java.io.FilterWriter(我们用来写入文件)并重写 write() 方法以抛出异常每当它尝试写入已删除的文件时。这似乎是一个很好的解决方案,但我找不到 Java 消化某些异常(当它尝试写入已删除的文件时)的任何地方,我们可以通过覆盖该方法来抛出这些异常。

Our java program writes some important data into a file continuously. Now we want to thrown some exception or display some kind of error message on the console whenever the file (to which the java program is writing) is deleted by some user manually.

I found that java doesn't throw any exception when it tries to write to a file which has been deleted.

Method I - Start a new thread which will keep checking whether the file is there and notify the main java program about it when the file has been deleted. This won't work in our case as we will be having several files to which the java program will be writing so starting a new thread for every file will not be memory efficient.

Method II - Check for the existence of the file every time just before writing to the file. It is also not a good approach because checking the file existence is a costlier operation.

Method III - Extend java.io.FilterWriter(which we are using to write to a file) and override write() method to throw exception whenever it tries to write to a file that has been deleted. This seems to be a good solution but I couldn't find any place where Java is digesting some exception(when it tries to write to a file which has been deleted) which we can throw by overriding that method.

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

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

发布评论

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

评论(4

无妨# 2024-11-23 22:06:18

FileChannel 和 < a href="http://download.oracle.com/javase/1.4.2/docs/api/java/nio/channels/FileLock.html" rel="nofollow noreferrer">FileLock 类在这种情况下可能会有所帮助。

请参阅如何使用 java 锁定文件(如果可能)

The FileChannel and FileLock classes could be helpful in this case.

see How can I lock a file using java (if possible)

燃情 2024-11-23 22:06:18

在Unix下,有文件,也有目录。一个文件可以出现在任意多个目录中,并且打开的文件可以位于磁盘上但不会出现在任何目录中。如果从目录中删除文件,则更改的是目录,而不是文件。

要确定目录是否已更改,您需要使用 File.exists() 轮询其路径。 Java 7 有一个目录观察器,可以避免轮询的需要。

Under Unix, there are files and there are directories. A file can appear in as many directories as you like, and an open file can be on disk but not appear in any directory. If a file is removed from a directory that is a change to the directory, not the file.

To determine if the directory has changed, you need to poll its path with File.exists(). Java 7 has a Directory Watcher which may avoid the need for polling.

断肠人 2024-11-23 22:06:18

首先,我怀疑,如果您尝试写入丢失的文件,java不会抛出异常。可能发生的情况是,在 unix 下,文件被用户删除,但进程仍然写入备份文件(自己的副本)

看看 apache commons io 文件监视器包:
http://commons .apache.org/proper/commons-io/apidocs/org/apache/commons/io/monitor/package-summary.html
基本上,您可以为不同的事件(文件删除、目录删除等)和监听器注册监听器。

如果你想阻止删除我会看看@splash的评论。
另一种选择是写入您自己的临时目录中的隐藏文件。

First of all, I doubt, that java won't throw an exception if you try to write to a missing file. What can happen is that under unix the file gets deleted by the user but the process still writes to a backup file (own copy)

Take a look at the apache commons io file monitor package:
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/monitor/package-summary.html
Basically, you can register listeners for different events (file delete, directory delete, etc.) and the listener.

If you want to prevent the deletion I will take a look at the comment of @splash.
Another option is to write to a hidden file in a temp directory of your own.

过期情话 2024-11-23 22:06:18
  1. 在 Unix、Linux、Solaris、MacOS、HP/UX、AIX 等中,无法检测打开的文件是否已被删除。

  2. 在 Windows 中,无法删除打开的文件。

所以你的问题没有明显的答案。

  1. In Unix, Linux, Solaris, MacOS, HP/UX, AIX, etc, there is no possible way to detect that an open file has been deleted.

  2. In Windows it is impossible to delete an open file.

So your question has no apparent answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文