Maybe you forgot to close the stream to that file somewhere in your application?
–
MaxJul 12 '11 at 11:07

maybe someone(user,application) use that folder sometimes when you try to delete.
–
ZemzelaJul 12 '11 at 11:08

@tul, you have almost 30 questions without an accepted answer. Perhaps you can try asking questions which can reasonably be answered or follow up answers so they can be accepted. ;)
–
Peter LawreyJul 12 '11 at 11:09

4 Answers
4

I'd suggest you use the Guava library. It has a method Files.createTempDir() that does exactly what you seem to need:

Atomically creates a new directory somewhere beneath the system's
temporary directory (as defined by the java.io.tmpdir system
property), and returns its name. Use this method instead of
File.createTempFile(String, String) when you wish to create a
directory, not a regular file. A common pitfall is to call
createTempFile, delete the file and create a directory in its place,
but this leads a race condition which can be exploited to create
security vulnerabilities, especially when executable files are to be
written into the directory. This method assumes that the temporary
volume is writable, has free inodes and free blocks, and that it will
not be called thousands of times per second.

Ok thanks for the tip will give it a try! I really need this to be deterministic. Currently it only works sometimes even though I go through the exact same steps (save/load the same folders/files) seems very random.
–
u123Jul 12 '11 at 12:06

Ok I have tried to replace the my implementation with the guava one but my problem persist. For some reason a file (image.jpg) in the temp dir cannot be deleted half the times I run the test - the other half it works fine. Must be some resource has it or its not disposed correctly before deleted.
–
u123Jul 12 '11 at 13:23

@tul yes, make sure you close all streams (in and out) in finally blocks. You might want to look at Closeables for that.
–
Sean Patrick FloydJul 12 '11 at 13:28

Basically I just set an org.eclipse.swt.graphics.Image.Image on a button like: myImage = new Image(getDisplay(), new ImageData(absolutePath)); button.setImage(myImage); Before doing the load I make sure to the container widget is closed and all sub widgets/images are disposed. But what still bother me is that it only occurs with a 50% chance.
–
u123Jul 12 '11 at 14:41

@tul can't help you with SWT. You should ask that part in another question with the SWT tag
–
Sean Patrick FloydJul 12 '11 at 14:47

As I understand org.apache.commons.io.FileUtils.deleteDirectory(file) deletes a folder recursively. Is it possible from java code to get a list of processes that has a reference to specific file/folder?
–
u123Jul 12 '11 at 11:44

1

You can in Unix with lsof. It is more than likely it is your process which has the files open. esp if they are temporary files.
–
Peter LawreyJul 12 '11 at 12:02

But thats exactly what org.apache.commons.io.FileUtils.deleteDirectory(file) does. An I need the deleteOnExit since I don't want the temp dir to grow insanely large - how would you else perform a cleanup?
–
u123Jul 12 '11 at 11:33

deleteOnExit won't help you keep the temp dir to a minimal size. If you want to do this you can write a simple thread that monitors the dirs size and deletes it if the size passes a certain threshold. Or if you are using apache commons you can install a FileMonitor to watch the directory... using deleteOnExit is bad, as it can leak memory and it does not guarantee that it will succeed.
–
peshkiraJul 12 '11 at 11:41

yeah it seems that deleteOnExit only works for empty folders :-(
–
u123Jul 12 '11 at 15:05