Java.io.File.createTempFile() Method
Description
The java.io.File.createTempFile(String prefix, String suffix) method atomically creates an empty file in the default temporary folder.
Declaration
Following is the declaration for java.io.File.createTempFile(String prefix, String suffix) method:
public static File createTempFile(String prefix, String suffix)
Parameters
prefix -- The prefix string defines the files name; must be at least three characters long
suffix -- The suffix string defines the file's extension; if null the suffix ".tmp" will be used
Return Value
An abstract pathname for the newly-created empty file.
Exception
IllegalArgumentException -- If the prefix argument contains less than three characters
IOException -- If a file creation failed
SecurityException -- If SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created
Example
The following example shows the usage of java.io.File.createTempFile(String prefix, String suffix) method.
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
try{
// creates temporary file
f = File.createTempFile("tmp", ".txt");
// prints absolute path
System.out.println("File path: "+f.getAbsolutePath());
// creates temporary file
f = File.createTempFile("tmp", null);
// prints absolute path
System.out.print("File path: "+f.getAbsolutePath());
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
File path: C:\Users\TP\AppData\Local\Temp\tmp2447618135336474361.txt File path: C:\Users\TP\AppData\Local\Temp\tmp1783337266599428081.tmp