Java Temp Directory Create makeTemporaryDirectory(File baseDir, String prefix)

Here you can find the source of makeTemporaryDirectory(File baseDir, String prefix)

Description

Creates a randomly-named temporary directory.

License

Apache License

Parameter

Parameter Description
baseDir base directory to contain the new directory. May be null , in which case the directory given by the java.io.tmpdir system property will be used.
prefix the initial characters of the new directory name

Return

a newly-created temporary directory; the caller must delete this directory (either when done or on VM exit)

Declaration

public static File makeTemporaryDirectory(File baseDir, String prefix) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w w  w  . j a va 2s.  c  om
 * Copyright 2006 Google 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;

import java.io.IOException;

public class Main {
    /**
     * Creates a randomly-named temporary directory.
     *
     * @param baseDir base directory to contain the new directory. May be
     *          {@code null}, in which case the directory given by the
     *          {@code java.io.tmpdir} system property will be used.
     * @param prefix the initial characters of the new directory name
     * @return a newly-created temporary directory; the caller must delete this
     *          directory (either when done or on VM exit)
     */
    public static File makeTemporaryDirectory(File baseDir, String prefix) throws IOException {
        if (baseDir == null) {
            baseDir = new File(System.getProperty("java.io.tmpdir"));
        }
        // No need to check the result of this mkdirs call because
        // we will detect the subsequent failure
        baseDir.mkdirs();

        // Try this a few times due to non-atomic delete+mkdir operations.
        for (int tries = 0; tries < 3; ++tries) {
            File result = File.createTempFile(prefix, null, baseDir);
            if (!result.delete()) {
                throw new IOException("Couldn't delete temporary file " + result.getAbsolutePath()
                        + " to replace with a directory.");
            }
            if (result.mkdirs()) {
                // Success.
                return result;
            }
        }
        throw new IOException("Couldn't create temporary directory after 3 tries in " + baseDir.getAbsolutePath());
    }
}

Related

  1. makeTempDir(String prefix)
  2. makeTempDir(String prefix, String suffix)
  3. makeTempDirectory(String dirName, boolean deleteIfExists)
  4. makeTempDirectory(String prefix, String suffix)
  5. makeTemporaryDirectory()
  6. makeTemporaryDirectory(String dir)