私は、JVMごとに常に一意の名前で終わる一時ファイルを生成するための絶対確実な方法を探しています。基本的に、マルチスレッドアプリケーションで、2つ以上のスレッドがまったく同じ時点で一時ファイルを作成しようとすると、両方が一意の一時ファイルになり、例外がスローされないことを確認したいと思います。
これは私が現在持っている方法です:
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;
}
これは私の目標にとって完全に安全ですか?以下のURLのドキュメントを確認しましたが、よくわかりません。
java.io.File#createTempFileを参照してください