Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible with Java to delete to the Recycle Bin?

Java is the key here. I need to be able to delete files but users expect to be able to "undelete" from the recycle bin. As far as I can tell this isn't possible. Anyone know otherwise?

like image 356
drye Avatar asked Oct 21 '08 16:10

drye


People also ask

How can I permanently delete a file in Java?

In Java, we can delete a file by using the File. delete() method of File class. The delete() method deletes the file or directory denoted by the abstract pathname. If the pathname is a directory, that directory must be empty to delete.

Can I delete Java files?

Delete Temporary Files through the Java Control Panel Click Delete Files on the Temporary Files Settings dialog. The Delete Files and Applications dialog box appears. Click OK on the Delete Files and Applications dialog. This deletes all the Downloaded Applications and Applets from the cache.

Does recycling bin automatically delete?

With Windows 10 you can automatically empty the Recycle Bin, but still keeping the most recent files (just in case) — here's how. On Windows 10, when you delete files, they're not immediately removed from the hard drive. Instead, they're sent to the Recycle Bin, in case you need to restore them.


2 Answers

Ten years later, with Java 9, finally there is a builtin way to move files to the Trash Bin

java.awt.Desktop.moveToTrash(java.io.File):

public boolean moveToTrash​(File file)

Moves the specified file to the trash.

Parameters:

file - the file

Returns:

returns true if successfully moved the file to the trash.

The availability of this feature for the underlying platform can be tested with Desktop.isSupported​(Desktop.Action.MOVE_TO_TRASH).

like image 90
Holger Avatar answered Sep 24 '22 21:09

Holger


For various reasons Windows has no concept of a folder that simply corresponds to the Recycle Bin.

The correct way is to use JNI to invoke the Windows SHFileOperation API, setting the FO_DELETE flag in the SHFILEOPSTRUCT structure.

  • SHFileOperation documention
  • Java example for copying a file using SHFileOperation (the Recycle Bin link in the same article doesn't work)
like image 36
John Topley Avatar answered Sep 23 '22 21:09

John Topley