Java - 如何在写入文件时检测文件删除?
我们的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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)
在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.
首先,我怀疑,如果您尝试写入丢失的文件,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.
在 Unix、Linux、Solaris、MacOS、HP/UX、AIX 等中,无法检测打开的文件是否已被删除。
在 Windows 中,无法删除打开的文件。
所以你的问题没有明显的答案。
In Unix, Linux, Solaris, MacOS, HP/UX, AIX, etc, there is no possible way to detect that an open file has been deleted.
In Windows it is impossible to delete an open file.
So your question has no apparent answer.