temporary files : Java Glossary
home T words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2007-03-29 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
temporary files
Java 1.2+ has File.createTempFile( String prefix, String suffix, File directory) for creating uniquely named temporary files. Unfortunately you still have to manually delete these files when you are finished with them.

Unlike C, Java 1.1 does not have methods for generating unique temporary filenames. I have written a getTempFile method.

Here is a slightly smarter version of Java 1.2+’s File.createTempFile() that will let you specify either a directory to create the temp file in, or a file in whose directory to create the file in. You need the logic of this smarter version if you want to write a utility that replaces the input file with the processed output from a renamed temp file.

I am using Vista and I am horrified to discover than even after 25 years Microsoft still does not understand the temporary files problem.

When a program crashes, it does not delete its temporary files. Over the years your disk fills with junk.

Windows does not:

  1. automatically clean up orpdaned temporary files after a crash.
  2. mark the temporary files in some way with a naming convention or attribute bit so that they can be recognised by some batch scavenging program run periodically.
Java is just as stupid. It is time at least to at least get Java capable of cleaning up its own mess. I suggest some official Sun format for temporary files, e.g. ~myappxxxxxxxx.temp. If all apps use that convention, a simple scavenger program, such as Ace Utilities, can scan the disk and mindlessly delete all *.temp files. That way you could also tell which app was leaving orphan temporaries behind. It is a symptom of pathology.

Here are three ways we could handle the probem of orphaned temporary files:

  1. Java has File.deleteOnExit(). In theory the OS should note this call and do the deletions even if Java crashes. The catch is, it doesn’t. If the app crashes, the Java runtime won’t necessarily be alive to do the deletions.
  2. The Linux way. More intelligent operating systems such as Linux and FreeBSD tend to clean /tmp and /var/tmp during the initial stages of a reboot, or, if one uses tmpfs, the issue doesn’t even arise since tmpfs is purely RAM-based and is wiped when the machine reboots.

    To use tmpfs, make sure your kernel can handle 'tmpfs', then add the following line into /etc/fstab:

    tmpfs /tmp tmpfs noatime,mode=1777 0 0 then either reboot or 'init s’ then 'init {runlevel}', depending on preferences and OS configuration. (Don’t just mount /tmp unless you’re sure nothing is using it. X in particular sticks files in there — /tmp/.X11-unix/Xn, presumably for its Unix sockets.)

  3. In a sane OS, temporary should be an attribute bit on the file the OS maintains. The OS could then automatically delete any file with that attribute not currently in use. Then you would need ways in each language to set the attribute. Java could access the attribute with a native method called by File. createTempFile(). For debugging, you could configure the OS scavenging to delay 24 hours to give you time to analyse the temp files after a crash. An app that terminated normally would automatically have all its temp files deleted, even if the app itself failed to do that. Vista now supports a temporary file attribute, but it does not automatically delete such files. You must run a utility periodically to find and delete them.

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] The information on this page is for non-military use only.
You are visitor number 11,162. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/temporaryfiles.html J:\mindprod\jgloss\temporaryfiles.html