This week's book giveaway is in the Testing forum.
We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!
See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File objects and deleting/recycling files

Joseph Maddison
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I've been figuring out how to delete a file from a Java program, but have it go to the Windows Recycle Bin, instead of simply being deleted. I've actually got that going using a native method that calls a dll coded in C (thanks going to various examples on the Sun web site and elsewhere).

There were a few important gotcha's that I needed to figure out (such as you must provide an absolute path when recycling, or else it will simply function as a deletion, with no error or explanation. That's Windows' fault, of course.)

My main() test method was coded to check to see if a certain temp file exists, by creating a File object from the filename without a path, and calling exists(). If the file did not exist, the method would create the file and terminate. The second time the code is executed, it finds the file exists, gets the absolute path and calls the native method to delete the file. This by itself did not actually work. I got a Windows error message "error deleting file. Cannot read from the source file or disk."

It turns out that if I explicitly set the File object to null, and I call System.gc(), then calling the native recycle method works fine. It appears as though creating the File object using the File(String) constructor apparently locks the file if it exists and it stays locked until the File object is garbage collected.

Can anyone explain what's really going on, and if there's a way to avoid locking the file in the first place, or else some way other than requesting garbage collection (which is not guaranteed to always execute), to allow it to be removed by native method?

In fact, while I've been writing this up, I've been verifying the details. I commented out the assignment of the File reference to null and the call to garbage collection, to reproduce the error, which reappeared as expected. Unfortunately, reversing the process didn't cause the problem to go away. Strange.

Even when I restored the source and class files from backup, the error wouldn't go away. I kept fiddling with it and discovered that calling Thread.sleep(1), (yes, for 1 millisecond), after the null assignment and the call for garbage collection, now seems to work fine once more.

So, fingers crossed, this appears to be working, but I'd really like to understand what's really going on. I can post the source code, if that would help-- just let me know.

[ December 02, 2004: Message edited by: Joseph Maddison ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic