Java Delete to Recycle Bin


It’s interesting that even a small shift in the desktop paradigm hasn’t been picked up by Java: the Recycle Bin. All of the popular operating systems have a variation of it but the only thing you can do in Java is permanently delete a file.

This is pretty inconvenient for AudioMan, which is trying to mimic Windows Explorer in some ways. So when I delete a track I want it to show up in the Recycle Bin. It’s not as simple as moving the file to the Recycle Bin folder, which is hidden. There are other things that have to be done — on Windows it’s accessible via a Win32 API call: SHFileOperation().

So far the only solution I’ve found is to use JNI but that’s not appealing to me as a Java developer. Wouldn’t it be nice if SWT did this for me on Windows, Mac OS and Linux like it does for opening files? Eventually the Java library should do it instead, but the SWT guys might be a bit faster implementing it.

Maybe I should just write a patch for SWT myself … I’ll have to dust off my copy of Visual C++ 6.0. Nevermind that, how do I implement it on Mac OS and Linux? Oh boy …

Update Monday 17:36: I submitted a bug report for “Move to Recycle Bin” to SWT and they replied that it is out of their scope, forwarding it to the Eclipse platform resources component. That’s pretty reasonable, and wasn’t completely unexpected.

They also suggested I ask Sun about including it in the File class (it’s not in the JavaDocs for Java 5). Yeah, wouldn’t that be nice? But who do I talk to about that? Is there a bug database? Yep, but I need to register. Are there Java bloggers I can talk to directly to find out why this hasn’t been done yet? Maybe the answer is really simple. Does a JSR need to be written for something this small? I’m in pretty foreign territory here. 🙂 Maybe Tim Bray knows someone I should talk to ….

… more legwork necessary. I’ll keep y’all updated.

Sun has a bug for this (login required) but hasn’t moved on it. The person reporting the bug wanted to replace delete() and deleteOnExit() with methods that send to the recycle bin. I agree with the rest of the people in the discussion that this is a bad idea. The top of the bug description says “Will break crossplatform compatibility. Wont fix.”

I agree, don’t change delete(). The File class just needs one method moveToRecycleBin() or moveToTrash() that can be used instead of delete(). For deletes in the GUI where the user expects to see the files moved to the recycle bin, it works. For other deletes (ie. configuration files that the user isn’t interested in) you need the permanent delete. They both have their place.

Another interesting idea is to write a new class that manages a recycle bin — moving files there, listing them and then recovering them. Now that all of the major platforms have the recycle bin paradigm, Java should get on the bandwagon and give us cross platform access to it, don’t you think?

Update Thursday July 29 10:04 PM: I have submitted an RFE bug to Sun for this. I’d link to it, but it won’t be visible on the bug site until it is reviewed. For Sun people who might be reading this, the RFE has the internal review ID 290373.