Java Temp Directory Create createTempDirectory(String prefix)

Here you can find the source of createTempDirectory(String prefix)

Description

Create a uniquely-named temporary directory.

License

Open Source License

Declaration

public static synchronized File createTempDirectory(String prefix) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.util.Random;

public class Main {
    public static final Random random = new Random();

    /**//from  w  w  w.j  a v  a 2s  . c o m
     * Create a uniquely-named temporary directory. This does essentially the
     * same thing as java.nio.file.Files#createTempDirectory, which we can't
     * use because it's a Java 7+ feature. Many Minecraft users are still on
     * Java 6.
     */
    public static synchronized File createTempDirectory(String prefix) {
        File baseDir = new File(System.getProperty("java.io.tmpdir"));
        baseDir.mkdirs();
        while (true) {
            File tempDir = new File(baseDir, prefix + Math.abs(random.nextLong()));
            if (!tempDir.exists()) {
                if (tempDir.mkdir()) {
                    return tempDir;
                }
            }
        }
    }
}

Related

  1. createTempDir(String suffix)
  2. createTempDirectory()
  3. createTempDirectory()
  4. createTempDirectory()
  5. createTempDirectory(File parent, String prefix)
  6. createTempDirectory(String prefix)
  7. makeTempDir()
  8. makeTempDir(String name)
  9. makeTempDir(String prefix)