Class TempFileManager


  • public class TempFileManager
    extends Object
    Generates and properly cleans up temporary files. Similar to File.createTempFile(java.lang.String, java.lang.String), this class provides a static method to create temporary files. The temporary files will be created in a special directory to be cleaned up the next time this class is loaded by the JVM. This functionality is required because Win32 platforms will not allow the JVM to delete files that are open. This causes problems with items such as JARs that get opened by a URLClassLoader and can therefore not be deleted by the JVM (including deleteOnExit).

    The caller should not need to create an instance of this class, although it is possible. Simply use the static methods to perform the required operations. Note that all files created by this class should be considered as deleted at JVM exit (although the actual deletion may be delayed). If persistent temporary files are required, use File instead.

    Refer to Sun bugs 4171239 and 4950148 for more details.

    Thanks to Mike Pilone for this code, which was sourced from http://www.devx.com/Java/Article/22018/ and contributed to Apache.

    • Constructor Detail

      • TempFileManager

        public TempFileManager()
    • Method Detail

      • createTempFile

        public static File createTempFile​(String prefix,
                                          String suffix)
                                   throws IOException
        Creates a temporary file in the proper directory to allow for cleanup after execution. This method delegates to File.createTempFile(java.lang.String, java.lang.String, java.io.File) so refer to it for more documentation. Any file created using this method should be considered as deleted at JVM exit; therefore, do not use this method to create files that need to be persistent between application runs.
        Parameters:
        prefix - the prefix string used in generating the file name; must be at least three characters long
        suffix - the suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
        Returns:
        an abstract pathname denoting a newly created empty file
        Throws:
        IOException - if a file could not be created
      • main

        public static void main​(String[] args)
        Utility method to load the TempFileManager at any time and allow it to clean the temporary files that may be left from previous instances
        Parameters:
        args - command line arguments are currently not supported