/** * create temporary file and register it to delete-on-exit hook. * File.deleteOnExit is not used for possible memory leakage. * * Make sure to use {@link #deleteTmpFile(File)} after the file is no longer required, * and has been deleted to avoid a memory leak. */ public static File createTempFile(String lScratchDir, String prefix, String suffix) throws IOException { File tmpDir = lScratchDir == null ? null : new File(lScratchDir); if (tmpDir != null && !tmpDir.exists() && !tmpDir.mkdirs()) { // Do another exists to check to handle possible race condition // Another thread might have created the dir, if that is why // mkdirs returned false, that is fine if (!tmpDir.exists()) { throw new RuntimeException("Unable to create temp directory " + lScratchDir); } } File tmpFile = File.createTempFile(prefix, suffix, tmpDir); ShutdownHookManager.deleteOnExit(tmpFile); return tmpFile; }
/** * create temporary file and register it to delete-on-exit hook. * File.deleteOnExit is not used for possible memory leakage. * * Make sure to use {@link #deleteTmpFile(File)} after the file is no longer required, * and has been deleted to avoid a memory leak. */ public static File createTempFile(String lScratchDir, String prefix, String suffix) throws IOException { File tmpDir = lScratchDir == null ? null : new File(lScratchDir); if (tmpDir != null && !tmpDir.exists() && !tmpDir.mkdirs()) { // Do another exists to check to handle possible race condition // Another thread might have created the dir, if that is why // mkdirs returned false, that is fine if (!tmpDir.exists()) { throw new RuntimeException("Unable to create temp directory " + lScratchDir); } } File tmpFile = File.createTempFile(prefix, suffix, tmpDir); ShutdownHookManager.deleteOnExit(tmpFile); return tmpFile; }