Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

File objects and deleting/recycling files

Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
I'm still in control here. LOOK at this tiny ad!
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic