Quintin Par
Quintin Par

Reputation: 16252

In Java: create unique random filename in a directory

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

Upvotes: 4

Views: 12067

Answers (2)

cletus
cletus

Reputation: 625057

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

File uniqueFile = File.createTempFile("post", ".html", new File("/var/www/doc"));

Upvotes: 14

Anon.
Anon.

Reputation: 59983

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

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

Upvotes: -8

Related Questions