Java - How to detect file deletion while writing to a file?
Asked Answered
I

4

11

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.

Insessorial answered 23/6, 2011 at 7:2 Comment(4)
Is this Windows or Unix? Are you closing the file after each write, or leaving it open the whole time you're running?Asis
@Asis It is Unix. In Windows you can't delete a file if it is being used by some process.Insessorial
In Unix there is no way to know that a file you have open has been deleted, so there's no way for Java (or any other language) to throw an exception when that happens.Asis
Maybe it's better to write the data in a database?Macbeth
M
1

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

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

Macbeth answered 23/6, 2011 at 7:4 Comment(3)
Does locking as you describe in any way prevent the locked file from being deleted?Asis
@Gabe: It seems that locking under Unix/Linux is only advisory and doesn't prevent deleting the file from other processes.Macbeth
Sometimes locking is mandatory (you can't actually use the part of the file that's locked), but even then I don't think it prevents you from deleting the locked file.Asis
A
1

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.

Ataghan answered 23/6, 2011 at 7:37 Comment(0)
M
1

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.

Menorah answered 23/6, 2011 at 9:10 Comment(0)
A
-1
  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.

Advisable answered 23/6, 2011 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.