title: How to use garbage collection to delete files?
body: <p>Hi I am using a lot of temporary files in java and my problem is that they do not get deleted.</p>
  
  <p>Without having to implement my own handling of temporary file management (not hard I grant you but I am lazy plus with so many things to do If I can save reinventing the wheel for this all the better) is there a way to ensure the temp files on disk will get deleted in a fairly regular fashion.</p>
  
  <p>1 - using File tmp = File.createTempFile(), sure I can say tmp.deleteOnExit() but If the thing runs in a service the only way it exits is when it either crashes (happens rarely), or when the system crashes (like when the drive is completely full of temp files and topples the cluster... oops !)</p>
  
  <p>Ideally, the instances created gets collected at some point by the garbage collector, and since there is a LOT of idle time in the application it would just be dandy if the GC could, well, finish it`s cleanup and actually delete the file on disk as well when dereferencing the instance from memory.</p>
  
  <p>the only way I see for now is overload the File class and add a finalized method...  If I do that might as well go with my own temp file manager !</p>
  
  <p><strong>So long story short, can I use the Garbage Collector to clean up system resources (ie files) as well ?</strong></p>
  
  <hr>
  
  <p>Thank you all for your answers.  I accepted Christoffer's as it was the simplest to implement and is what I ended up doing.</p>
  
  <p>I guess being cleaned up after for so many years made me forget the basic housekeeping I was though to do the hard way in the good'ol days of C++.</p>