Java Temp Directory Create createTempDir(String prefix)

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

Description

create Temp Dir

License

Apache License

Declaration

@Deprecated
public static File createTempDir(String prefix) 

Method Source Code


//package com.java2s;
/*//w  w  w  .  j  a  v a2s. c  om
 * Copyright 2010 Proofpoint, Inc.
 *
 * 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;

public class Main {
    private static final int TEMP_DIR_ATTEMPTS = 10000;

    /**
     * @deprecated Use {@link java.nio.file.Files#createTempDirectory(String, FileAttribute[])}
     */
    @Deprecated
    public static File createTempDir(String prefix) {
        return createTempDir(new File(System.getProperty("java.io.tmpdir")), prefix);
    }

    /**
     * @deprecated Use {@link java.nio.file.Files#createTempDirectory(Path, String, FileAttribute[])}
     */
    @Deprecated
    public static File createTempDir(File parentDir, String prefix) {
        String baseName = "";
        if (prefix != null) {
            baseName += prefix + "-";
        }

        baseName += System.currentTimeMillis() + "-";
        for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) {
            File tempDir = new File(parentDir, baseName + counter);
            if (tempDir.mkdir()) {
                return tempDir;
            }
        }
        throw new IllegalStateException("Failed to create directory within " + TEMP_DIR_ATTEMPTS
                + " attempts (tried " + baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')');
    }
}

Related

  1. createTempDir()
  2. createTempDir()
  3. createTempDir(final String prefix)
  4. createTempDir(String subdirectory)
  5. createTempDir(String suffix)
  6. createTempDirectory()
  7. createTempDirectory()