Java Temp File Create getUniqueTempFile(final boolean autoDelete, final File parentFolder, final String suffix)

Here you can find the source of getUniqueTempFile(final boolean autoDelete, final File parentFolder, final String suffix)

Description

get Unique Temp File

License

Apache License

Parameter

Parameter Description
autoDelete set to true if you want to have the file cleaned up on JVM exit, no guareentee but...
parentFolder Where to create the file. When null, uses default temp folder.
suffix Suffix for the file.

Exception

Parameter Description
IOException The file could not be created.

Return

A temporary file.

Declaration

public static File getUniqueTempFile(final boolean autoDelete, final File parentFolder, final String suffix)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    /**/*from  w  ww .j  av  a2s.c o m*/
     * @param autoDelete   set to true if you want to have the file cleaned up on JVM exit, no guareentee but...
     * @param parentFolder Where to create the file. When null, uses default temp folder.
     * @param suffix       Suffix for the file.
     * @return A temporary file.
     * @throws IOException The file could not be created.
     */
    public static File getUniqueTempFile(final boolean autoDelete, final File parentFolder, final String suffix)
            throws IOException {
        final File tmpFile = File.createTempFile(
                "test_" + new SimpleDateFormat("ddMMMyyyy-HHmmss").format(new Date()), suffix, parentFolder);
        if (autoDelete) {
            tmpFile.deleteOnExit();
        }
        return tmpFile;
    }
}

Related

  1. getUniqueFile(File dir, String prefix, String suffix)
  2. getUniqueFileName()
  3. getUniqueFilename(String filename, String extension)
  4. getUniqueFileName(String fileNamePrefix, String fileExtension)
  5. getUniqueFileName(String originalFileName)
  6. newTmpFile(String content)
  7. newTmpFile(String content, String encoding)