Java Temporary File Multithreaded Application
<p>I'm looking for a foolproof way to generate a temporary file that will have always end up with a unique name on a per JVM basis. Basically I want to be sure in a multithreaded application that if two or more threads attempt to create a temporary file at the exact same moment in time that they will both end up with a unique temporary file and no exceptions will be thrown.</p>

<p>This is the method I have currently:</p>

<pre><code>public File createTempFile(InputStream inputStream) throws FileUtilsException {
    File tempFile = null;
    OutputStream outputStream = null;
    try {
        tempFile = File.createTempFile("app", ".tmp");
        tempFile.deleteOnExit();
        outputStream = new FileOutputStream(tempFile);
        IOUtils.copy(inputStream, outputStream);
    } catch (IOException e) {
        logger.debug("Unable to create temp file", e);
        throw new FileUtilsException(e);
    } finally {
        try { if (outputStream != null) outputStream.close(); } catch (Exception e) {}
        try { if (inputStream != null) inputStream.close(); } catch (Exception e) {}
    }
    return tempFile;
}
</code></pre>

<p>Is this perfectly safe for what my goal is? I reviewed the documentation at the below URL but I'm not sure.</p>

<p>See <a href="http://docs.oracle.com/javase/6/docs/api/java/io/File.html#createTempFile%28java.lang.String,%20java.lang.String%29" rel="nofollow">java.io.File#createTempFile</a></p>
