Java Temp File Create getTempFile(final String directory, final String suffix, boolean autodelete)

Here you can find the source of getTempFile(final String directory, final String suffix, boolean autodelete)

Description

Creates a temporary file in the given directory, with the given suffix.

License

Open Source License

Parameter

Parameter Description
directory Directory to create the file in.
suffix File suffix/extension.
autodelete Indicator if the created file should be deleted when exiting the JVM.

Exception

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

Return

A object pointing to the new file.

Declaration

public static synchronized File getTempFile(final String directory,
        final String suffix, boolean autodelete) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  ww w.  j a v  a  2s. c  o  m*/
 * 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 file in the given directory, with the given suffix.
     * 
     * @param directory
     *            Directory to create the file in.
     * @param suffix
     *            File suffix/extension.
     * @param autodelete
     *            Indicator if the created file should be deleted when exiting
     *            the JVM.
     * @return A {@link File} object pointing to the new file.
     * @throws IOException
     *             In case of problems when creating the file.
     */
    public static synchronized File getTempFile(final String directory,
            final String suffix, boolean autodelete) throws IOException {

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

        if (!file.createNewFile()) {
            throw new IOException("Failed to create file "
                    + file.getAbsolutePath());
        }

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

        return file;
    }

    /**
     * Creates a temporary file in the systems temporary directory with the
     * given suffix.
     * 
     * @param suffix
     *            File suffix/extension.
     * @param autodelete
     *            Indicator if the created file should be deleted when exiting
     *            the JVM.
     * @return A {@link File} object pointing to the new file.
     * @throws IOException
     *             In case of problems when creating the file.
     */
    public static synchronized File getTempFile(final String suffix,
            boolean autodelete) throws IOException {
        File file = File.createTempFile("GKN", suffix);
        if (autodelete) {
            file.deleteOnExit();
        }
        return file;
    }
}

Related

  1. getTempFile()
  2. getTempFile()
  3. getTempFile(File file)
  4. getTempFile(File parent, String fileName)
  5. getTempFile(final File stackFile)
  6. getTempFile(final String name)
  7. getTempFile(String config)
  8. getTempFile(String extension)
  9. getTempFile(String fileId)