I am working on a Java desktop application in which I need to implement recycle bin functionality. Currently:
My problem is that when a file with the same name is deleted from different locations, all the files moved to the Recycle Bin folder created by me and the old file with the same names are overwritten.
How can I sort out this issue? Also, is there any other alternate way to get functionality similar to recycle bin?
When you delete a file:
(Note: this has issues if you have multiple applications/threads doing this at the same time.)
All the information you need to "restore" the file is in the text file, and you've avoided the duplicate name problem.
For more robustness against concurrent uses, and if you have Java 1.5 or greater, consider using a java.util.UUID
rather than a "dumb" random number as the recycled file name. Can't be guaranteed to be safe AFAIK, but it should be good enough in most circumstances (especially if there's only one real user on the machine this is running).
Lastly, make sure you've closed your output streams in steps 3 and 4 above before you delete, and that they didn't throw any exception - otherwise you risk losing data. (Same thing when you restore: close the restored file stream before deleting the recycled one.)
Whenever there is need to move a file to recycle bin,
Now when user asks for restore, get the exact location from txt using the key and restore it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With