4

How do I create a random unique filename in a directory (of my choice)?

Note: I don’t want this file in the system temp path but rather in the directory I specify

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Quintin Par
  • 15,862
  • 27
  • 93
  • 146
  • 2
    And did you look in the File class, perhaps at a method named createTempFile? – President James K. Polk Jan 13 '10 at 03:02
  • @JamesKPolk I did, and then I ended up here because I did not find any method in the `File` class that does that; certainly not `createTempFile`; that would be OK if I wanted to create the file, which is not what this question asks, regardless of whether the OP wants that or not. – SantiBailors Dec 01 '16 at 08:55

2 Answers2

14

File.createTempFile() allows you to specify a directory so:

File uniqueFile = File.createTempFile("post", ".html", new File("/var/www/doc"));
Koedlt
  • 4,286
  • 8
  • 15
  • 33
cletus
  • 616,129
  • 168
  • 910
  • 942
-8

You want the File.createTempFile(String, String, File) overload.

Which you would have seen immediately had you bothered to look at the documentation.

Anon.
  • 58,739
  • 8
  • 81
  • 86