Marc Hughes

I am a developer from a bit west of Boston.

AIR & Temp Files

30 Mar 2008

Do you write AIR applications?  Do you use temp files?  You do realize you need to delete those yourself, right?  File.createTempFile() and File.createTempDirectory() give an easy way to create temporary storage locations, but they aren't automatically cleaned up for you.

I've written a simple utility class that you can register those temporary files with and then delete them all at shutdown with a single simple call.  To use it, do one of the following:

1) Instead of calling File.createTempFile or File.createTempDirectory manually, use the TempFileUtil.getTempFile or TempFileUtil.getTempDirectory.  Those will work identically to the File.* methods, but the will also add the file to a list of locations that need to be cleaned up.

2) Or, after you do use File.createTemp*, call TempFileUtil.addTempFile.  This will do just the file registration part.

And then, at program shutdown, call the TempFileUtil.cleanup() method.  This will go through and delete all of the temporary files and directories that have been registered.  (Make sure you have closed those files or the OS won't let you delete them!)

Download: (2.4k Last Updated 3/30/2008)

Hope someone finds this useful.