Search code examples
javarandomfilenames

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


Solution

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

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