Java Temp Directory Get getTempDir()

Here you can find the source of getTempDir()

Description

Creates and returns a new randomly named temporary directory.

License

Apache License

Exception

Parameter Description
RuntimeException if a new temporary directory could not be created.

Return

a new temporary directory.

Declaration

public static File getTempDir() 

Method Source Code

//package com.java2s;
/**//  w ww  .  j ava  2s . com
 * Copyright 2015 Cerner Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.util.Random;

public class Main {
    private static final Random RANDOM = new Random();
    private static final String TEMP_DIR_PREFIX = "kafka-";

    /**
     * Creates and returns a new randomly named temporary directory. It will be deleted upon JVM exit.
     *
     * @return a new temporary directory.
     *
     * @throws RuntimeException if a new temporary directory could not be created.
     */
    public static File getTempDir() {
        File file = new File(System.getProperty("java.io.tmpdir"), TEMP_DIR_PREFIX + RANDOM.nextInt(10000000));
        if (!file.mkdirs()) {
            throw new RuntimeException("could not create temp directory: " + file.getAbsolutePath());
        }
        file.deleteOnExit();
        return file;
    }
}

Related

  1. getTempDir()
  2. getTempDir()
  3. getTempDir()
  4. getTempDir()
  5. getTempDir()
  6. getTempDir()