Java Temp Directory Get getTempDir(final String directory, final String prefix, boolean autodelete)

Here you can find the source of getTempDir(final String directory, final String prefix, boolean autodelete)

Description

Creates a temporary directory in the given directory with the given prefix.

License

Open Source License

Parameter

Parameter Description
directory The directory to create the directory in.
prefix The prefix of the generated directory.
autodelete If true the file will be deleted when JVM shuts down.

Exception

Parameter Description
IOException In case of problems when creating the directory.

Return

A pointing to the newly generated directory.

Declaration

public static synchronized File getTempDir(final String directory,
        final String prefix, boolean autodelete) throws IOException 

Method Source Code

//package com.java2s;
/**//from   ww w .java2s. c  om
 * Copyright (c) 2011, Marc R?ttig, Stephan Aiche.
 *
 * This file is part of GenericKnimeNodes.
 * 
 * GenericKnimeNodes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.IOException;

import java.util.Random;

public class Main {
    /**
     * Local random number generator to ensure uniqueness of file names.
     */
    private static Random randomNumberGenerator = new Random();

    /**
     * Creates a temporary directory in the given directory with the given
     * prefix.
     * 
     * @param directory
     *            The directory to create the directory in.
     * @param prefix
     *            The prefix of the generated directory.
     * @param autodelete
     *            If true the file will be deleted when JVM shuts down.
     * @return A {@link File} pointing to the newly generated directory.
     * @throws IOException
     *             In case of problems when creating the directory.
     */
    public static synchronized File getTempDir(final String directory,
            final String prefix, boolean autodelete) throws IOException {

        int num = randomNumberGenerator.nextInt(Integer.MAX_VALUE);
        File dir = new File(directory + File.separator
                + String.format("%s%06d", prefix, num));
        while (dir.exists()) {
            num = randomNumberGenerator.nextInt(Integer.MAX_VALUE);
            dir = new File(directory + File.separator
                    + String.format("%s%06d", prefix, num));
        }
        dir.mkdirs();

        if (autodelete) {
            dir.deleteOnExit();
        }

        return dir;
    }

    /**
     * Creates a temporary directory in the systems temporary directory with the
     * given prefix.
     * 
     * @param prefix
     *            The prefix of the generated directory.
     * @param autodelete
     *            If true the file will be deleted when JVM shuts down.
     * @return A {@link File} pointing to the newly generated directory.
     * @throws IOException
     *             In case of problems when creating the directory.
     */
    public static synchronized File getTempDir(final String prefix,
            boolean autodelete) throws IOException {
        return getTempDir(System.getProperty("java.io.tmpdir"), prefix,
                autodelete);
    }
}

Related

  1. getTempDir()
  2. getTempDir()
  3. getTempDir()
  4. getTempDir()
  5. getTempDir(File baseDir)
  6. getTempDir(Object fileID, String fileName)
  7. getTempDir(String desc)
  8. getTempDir(String name, String prefix, File parentDir)
  9. getTempDir(String path)