Using the Path API available from Java 7 you can generate a temporary Path file with a uniquely generated name.
The Files
utility class provides Files.createTempFile
, which works in a similar manner as File.createTempFile
, except it produces a Path object
So calling the method
Path baseDir = ...
Files.createTempFile(baseDir, "status-log-", ".log");
// //dir path, prefix , suffix
Will produce something similar to
C:\...\status-log-4746781128777680321.log
If you want to open the file and delete it after you're done with it, you can use DELETE_ON_CLOSE
, taken from the docs:
As with the File.createTempFile methods, this method is only part of a temporary-file facility. Where used as a work files, the resulting file may be opened using the DELETE_ON_CLOSE option so that the file is deleted when the appropriate close method is invoked. Alternatively, a shutdown-hook, or the File.deleteOnExit mechanism may be used to delete the file automatically.