Delete Temporary Files on Exit

Sometimes your application requires holding data in a temporary file. I’ve had to do this when printing a PDF file. The following bit of code demonstrates how to create a temporary file and delete said file when the program terminates.

File temp = File.createTempFile("SomePrefix", ".pdf");
temp.deleteOnExit();

The file is created in the system’s temp directory, see the Java java.io.tmpdir property.

Related posts:

  1. Make and Delete Directories
  2. Accessing Local Files With Mozilla XPCOM
  3. Reading Client-side Files with XPCOM
  4. Class HighLite File
  5. Print a PDF Document in Java

This entry was posted in Java, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*