Check if the file is open before deleting it

How to check if a file is open before deleting it? programmatically?

something like that

if (file is open){ // close it first } // delete file 
+4
source share
5 answers

I do not think this will work for several reasons.

  • There are no standard Java mechanisms for testing if you already have a file open.

  • Even if such a mechanism existed, it would be difficult to find a file descriptor so you can close it.

  • Even if you can find the file descriptor, there is a potential race condition when one thread checks the file and tries to delete it, and the second thread opens the file descriptor.

  • None of this applies when any other process has an open file.

If you have a problem with deleting files opened by your application, then most likely the real problem is that your application skips file descriptors. The best solution for this is to find and fix the leak ... or make sure that all your file streams, etc. Closed using try / finally or the new Java 7 try with resource construct.

On the other hand, if the file is opened by some other process, you can simply try to delete the file without testing to see if it is open. If the deletion succeeds, it will succeed. Otherwise, detect a failure and do everything you would if you found that the file was open.

+3
source

You may find the answer to check if the file is open , since your question is a replication of this question

+1
source

Basically, you need to keep track of whether you opened the file if you need to make sure it is closed.

Please note that on Unix systems, deleting an open file is not a problem and will work correctly.

0
source

You can try to lock the file, and if the file is not opened by someone else, you will not get an exception, otherwise you will get an exception, so by writing this function, you can check if the file is already opened by someone else:

 public boolean isFileOpened(File file) boolean res = false; FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); // Get an exclusive lock on the whole file FileLock lock = channel.lock(); try { //The file is not already opened lock = channel.tryLock(); } catch (OverlappingFileLockException e) { // File is open by someone else res = true; } finally { lock.release(); } return res; } 

Then you can use this method to check if the file is open this way:

 if(isFileOpened(someFile)) { //Do Something } 
0
source

Hi @chinchu and @Subash,

You cannot check directly, there is no API for this, you need to write code for it

Below is a snippet of code that you can modify as per your requirement:

  File file1 = new File("<file path>"); boolean isDeleteSuccess; do { //delete the file isDeleteSuccess = file1.delete(); if (isDeleteSuccess) System.out.println(file1.getName() + " is deleted!"); else { //if not deleted then wait for 10 seconds for file to be closed System.out.println("Delete operation is failed."); Thread.sleep(10000); } } while (!isDeleteSuccess);//if not deleted then try again 
0
source

Source: https://habr.com/ru/post/1387995/


All Articles