Java - File createTempFile(String prefix, String suffix, File directory) method



Description

The Java File createTempFile(String prefix, String suffix, File directory) method creates a new empty file in the specified directory. deleteOnExit() method is called to delete the file created by this method.

Declaration

Following is the declaration for java.io.File.createTempFile(String prefix, String suffix, File directory) method −

 public static File createTempFile(String prefix, String suffix, File directory) 

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

  • directory − The directory in which the file is to be created. For default temporary-file directory null is to passed

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 - Usage of File createTempFile(String prefix, String suffix, File directory) method

The following example shows the usage of Java File createTempFile(String prefix, String suffix, File directory) method. We've created a File reference. Using createTempFile() method, we're creating the file in given directory and printing the absolute path of the file created. We're passing both parameters to createTempFile() as tmp and .txt to create the temporary file with prefix as tmp and suffix as .txt.

FileDemo.java

 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", new File("F:/")); // prints absolute path System.out.println("File path: "+f.getAbsolutePath()); } catch(Exception e) { // if any error occurs e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 File path: F:\tmp11479871496507253874.txt 

Example - Usage of File createTempFile(String prefix, String suffix, File directory) method

The following example shows the usage of Java File createTempFile(String prefix, String suffix, File directory) method. We've created a File reference. Using createTempFile() method, we're creating the file in given directory and printing the absolute path of the file created.

We're passing both parameters to createTempFile() as tmp and null to create the temporary file with prefix as tmp and suffix as .tmp.

FileDemo.java

 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", null, new File("F:/")); // prints absolute path System.out.println("File path: "+f.getAbsolutePath()); } catch(Exception e) { // if any error occurs e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 File path: F:\tmp11732077494906020658.tmp 

Example - Usage of File createTempFile(String prefix, String suffix, File directory) method

The following example shows the usage of Java File createTempFile(String prefix, String suffix, File directory) method. We've created a File reference. Using createTempFile() method, we're creating the file in given directory and printing the absolute path of the file created. We're passing both parameters to createTempFile() as tm and .txt to create the temporary file with prefix as tm and suffix as .text. It is to check if we're getting exception or not for too short prefix name.

FileDemo.java

 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("tm", ".txt", new File("F:/")); // prints absolute path System.out.println("File path: "+f.getAbsolutePath()); } catch(Exception e) { // if any error occurs e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 java.lang.IllegalArgumentException: Prefix string "tm" too short: length must be at least 3	at java.base/java.io.File.createTempFile(File.java:2104)	at java.base/java.io.File.createTempFile(File.java:2175)	at FileDemo.main(FileDemo.java:9) 
java_io_file_methods.htm
Advertisements