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.

Amit
  • 33,847
  • 91
  • 226
  • 299
  • Is this Windows or Unix? Are you closing the file after each write, or leaving it open the whole time you're running? – Gabe Jun 23 '11 at 07:05
  • @Gabe It is Unix. In Windows you can't delete a file if it is being used by some process. – Amit Jun 23 '11 at 07:08
  • 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. – Gabe Jun 23 '11 at 07:09
  • Maybe it's better to write the data in a database? – splash Jun 23 '11 at 07:38

4 Answers4

1

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

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

Community
  • 1
  • 1
splash
  • 13,037
  • 1
  • 44
  • 67
  • Does locking as you describe in any way prevent the locked file from being deleted? – Gabe Jun 23 '11 at 07:12
  • 1
    @Gabe: It seems that locking under Unix/Linux is only advisory and doesn't prevent deleting the file from other processes. – splash Jun 23 '11 at 07:33
  • 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. – Gabe Jun 23 '11 at 07:37
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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
peshkira
  • 6,069
  • 1
  • 33
  • 46
-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.

user207421
  • 305,947
  • 44
  • 307
  • 483