173

I'm looking for a safe way to create a temp file in Java. By safe, I mean the following:

  • Name should be unique, even under potential race conditions (e.g. another Thread calls the same func at the same time, or another process runs this code simultaneously)
  • File should be private, even under potential race conditions (e.g. another user tries to chmod file at high rate)
  • I can tell it to delete the file, without me having to do a generic delete, and risk deleting the wrong file
  • Ideally, should ensure file is deleted, even if exception is thrown before I get the chance to
  • File should default to a sane location (e.g. the JVM specified tmp dir, defaulting to the system temp dir)
3
  • 6
    Use File f = File.createTempFile(prefix, extension). It will be placed in the temp dir. And with f.deleteOnExit() it will be automatically deleted on exit.
    – BackSlash
    Commented Nov 11, 2014 at 8:01
  • 4
    Delete on Exit will not delete the file, if the vm exits with an exception.
    – Johannes
    Commented May 14, 2017 at 18:18
  • 1
    Use Files.createTempFile("tempfiles", ".tmp") instead, for enhanced security; check 2nd most voted answer.
    – Ricardo
    Commented Dec 1, 2022 at 1:39

2 Answers 2

219

Use File.createTempFile().

File tempFile = File.createTempFile("prefix-", "-suffix");
//File tempFile = File.createTempFile("MyAppName-", ".tmp");
tempFile.deleteOnExit();

Will create a file in the temp dir, like:

prefix-6340763779352094442-suffix

7
  • 3
    Just FYI, you dont need to worry about having a unique prefix / suffix, since Java will create a random String of numbers in between.
    – Squeazer
    Commented Nov 11, 2014 at 8:04
  • 5
    Super! Any problem doing a normal tempFile.delete() in addition to the deleteOnExit (since I don't want to have hordes of temp files hanging around before exit)? Commented Nov 11, 2014 at 8:10
  • 4
    No, you can do it either way.
    – Stefan
    Commented Nov 11, 2014 at 8:11
  • 1
    @SRobertJames:No problems. The thing is that the creation of tempfiles with predictable names imposes security problems. Once they are created in a safe way with proper permissions, they don't. Commented Nov 11, 2014 at 8:13
  • 8
    I wouldn't consider this safe, the created file is world-readable and it resides in the tmp directory which tends to be accessible to any user. Commented Aug 11, 2017 at 19:48
72

Since Java 7 there is the new file API "NIO2" which contains new methods for creating temp files and directories. See

e.g.

Path tempDir = Files.createTempDirectory("tempfiles");

or

Path tempFile = Files.createTempFile("tempfiles", ".tmp");

Security notice:

An important difference between File.createTempFile() and Files.createTempFile is also that the latter has more secure permission defaults.

When no file attributes are specified, then the resulting file may have more restrictive access permissions to files created by the File.createTempFile(String,String,File) method.

2
  • 1
    I prefer NIO, which doesn't require a prefix. Commented Oct 11, 2017 at 15:55
  • @FranklinYu How did you manage not to specify a prefix tho?
    – Thomas
    Commented Mar 20 at 17:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.