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,576
  • 13
  • 42
  • 57
Quintin Par
  • 15,424
  • 27
  • 90
  • 143
  • 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"));
cletus
  • 599,013
  • 161
  • 897
  • 938
-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.
  • 56,053
  • 8
  • 78
  • 84