File createTempFile() method in Java with Examples
The createTempFile() function creates a temporary file in a given directory ( if directory is not mentioned then a default directory is selected ), the function generates the filename by using the prefix and suffix passed as the parameters . If the suffix is null then the function uses “.tmp” as suffix. The function then returns the created file
Function signature:
1. public static File createTempFile(String PREFIX, String SUFFIX)
OR
2. public static File createTempFile(String PREFIX, String SUFFIX, File DIR)
Syntax:
1. File.createTempFile(String, String, FILE); 2. File.createTempFile(String, String);
Parameters:The function is a overloaded function so one function takes suffix, prefix and a File object, whereas other function takes only suffix and prefix.The prefix must not be less than three characters but the suffix might be null and if the directory is not specified or a null value is passed then the function uses an default directory.
Return Type: The function returns the abstract file name denoting the newly created temporary file
Exception: This method throws:
- IllegalArgumentException: if the prefix argument contains less than three characters
- IOExcetion: if there is any IO error(File could not be created)
- SecurityException: if the method does not allow file to be created
Below programs illustrates the use of createTempFile() function:
Example 1: If we provide the prefix string and provide null suffix string
// Java program to demonstrate // createTempFile() method of File Class import java.io.*; public class solution { public static void main(String args[]) { try { // create a temp file File f = File.createTempFile("geeks", null); // check if the file is created if (f.exists()) { // the file is created // as the function retruned true System.out.println("Temp File created: " + f.getName()); } else { // display the file cannot be created // as the function returned false System.out.println("Temp File cannot be created: " + f.getName()); } } catch (Exception e) { // display the error message System.err.println(e); } } } |
Output:
Temp File created: geeks7503529537487244659.tmp
Example 2: If we provide the prefix string and a definite suffix string
// Java Program to demonstrate // createTempFile() method import java.io.*; public class solution { public static void main(String args[]) { try { // create a temp file File f = File.createTempFile("geeks", ".SVP"); // check if the file is created if (f.exists()) { // the file is created // as the function retruned true System.out.println("Temp File created: " + f.getName()); } else { // display the file cannot be created // as the function returned false System.out.println("Temp File cannot be created: " + f.getName()); } } catch (Exception e) { // display the error message System.err.println(e); } } } |
Output:
Temp File created: geeks4425780422923344328.SVP
Example 3: If we provide the prefix string, a definite suffix string and a directory
// Java Program to demonstrate // createTempFile() method import java.io.*; public class solution { public static void main(String args[]) { try { // create a temp file File f = File.createTempFile("geeks", ".SVP", new File("F:")); // check if the file is created if (f.exists()) { // the file is created // as the function retruned true System.out.println("Temp File created: " + f.getAbsolutePath()); } else { // display the file cannot be created // as the function returned false System.out.println("Temp File cannot be created: " + f.getAbsolutePath()); } } catch (Exception e) { // display the error message System.err.println(e); } } } |
Output:
Temp File created: F:\geeks7006753451952178741.SVP
Note: The programs might not run in an online IDE. Please use an offline IDE and set the path of the file.
Recommended Posts:
- File getCanonicalPath() method in Java with Examples
- File length() method in Java with Examples
- File exists() method in Java with examples
- File isFile() method in Java with Examples
- File createNewFile() method in Java with Examples
- File lastModified() method in Java with Examples
- File getAbsoluteFile() method in Java with Examples
- File getUsableSpace() method in java with examples
- File getTotalSpace() method in Java with examples
- File getFreeSpace() method in Java with examples
- File getAbsolutePath() method in Java with Examples
- File delete() method in Java with Examples
- File isHidden() method in Java with Examples
- File setLastModified() method in Java with Examples
- File setWritable() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



