Java Utililty Methods Temp File Create

List of utility methods to do Temp File Create

Description

The list of methods to do Temp File Create are organized into topic(s).

Method

StringgetUniqueFilename(String filename, String extension)
get Unique Filename
return filename + "_" + getCurrentDateAsString("yyyy_MM_dd_HH_mm_ss_SSS") + "." + extension;
StringgetUniqueFileName(String fileNamePrefix, String fileExtension)
get Unique File Name
Date d = Calendar.getInstance().getTime();
long now = System.currentTimeMillis();
return "QTIScoringEngine_" + fileNamePrefix + "_"
        + DateFormat.getDateInstance(DateFormat.SHORT).format(d).replace("/", "-") + "_"
        + DateFormat.getTimeInstance(DateFormat.LONG).format(d).replace(" ", "").replace(":", "_")
        + fileExtension;
StringgetUniqueFileName(String originalFileName)
Gets a unique file name, based on the fileName with the time in milliseconds added before the file extension., This works with all file types including directories.
int index = originalFileName.lastIndexOf('.');
if (index < 0)
    index = originalFileName.length();
String suffix = originalFileName.substring(index);
originalFileName = originalFileName.replace(suffix, "");
String newFileName = originalFileName + "_" + getDateTimeMilliSecondEnsureUnique() + suffix;
return newFileName;
FilegetUniqueTempFile(final boolean autoDelete, final File parentFolder, final String suffix)
get Unique Temp File
final File tmpFile = File.createTempFile(
        "test_" + new SimpleDateFormat("ddMMMyyyy-HHmmss").format(new Date()), suffix, parentFolder);
if (autoDelete) {
    tmpFile.deleteOnExit();
return tmpFile;
FilenewTmpFile(String content)
Creates a new temporary file using the default encoding of ISO-8859-1 (Latin1).
return newTmpFile(content, "ISO-8859-1");
FilenewTmpFile(String content, String encoding)
Makes a new temporary file and writes content into it.
Writer out = null;
Reader in = null;
File f = null;
try {
    f = File.createTempFile("jspwiki", null);
    in = new StringReader(content);
    out = new OutputStreamWriter(new FileOutputStream(f), encoding);
    copyContents(in, out);
...