Pregunta

I am calling a C++ Method via JNI which creates two files. A text log file and a pdf file in a given directory. I want to delete these files (if they exist) before executing the JNI method.

I am using Apache commons.io (FileUtils.forceDelete(File file)) for that. When I execute I get an IOException:

java.io.IOException: Unable to delete file: D:\Folder\file.log

I check the writable state of the file before fireing the delete method with the File.canWrite() method. It returns true for both the file and the parent dir.

Do you have an idea why I have problems deleting the file? As far as I know the C++ method which is creating the files is closing or unlocking them after the method finishes. Anyway, I don't have access to the source code of the C++ code so I can't check if that is really the case or modify the code.

Thanks, Marco

¿Fue útil?

Solución

It is almost certainly locked by another process. If it is another process locking at the OS level (say you had the file open it a text editor) then you won't have much luck. Even windows explorer can fail to delete a file if something else is locking it. However have a look at java.nio.channels.FileLock for the relevant API calls.

Otros consejos

Most likely, another process is keeping file.log open, which would prevent it from being deleted.

I'm running Eclipse 4.x and jre 1.7, webapp deployed to tomcat7.

I have the same problem attempting to delete files from within my spring controller.

File f = new File("/home/me/my/file/liveshere/smallfile.txt");
f.delete();

It doesn't work from within the webapp. But... it works in a standalone java app, run from the same IDE.

Perhaps Tomcat is locking the folder, since I did read earlier(but only by instantiating File() class). I'm not explicitly using streams.

I also tried using commons.io library to FileUtils.forceDelete(f), but still had no joy.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top