
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - File createTempFile(String prefix, String suffix) method
Description
The Java 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 - Usage of File createTempFile(String prefix, String suffix) method
The following example shows the usage of Java File createTempFile(String prefix, String suffix) method. We've created a File reference. Using createTempFile() method, we're creating the file in temporary 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"); // 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: C:\Users\intel\AppData\Local\Temp\tmp3526113184482496754.txt
Example - Usage of File createTempFile(String prefix, String suffix) method
The following example shows the usage of Java File createTempFile(String prefix, String suffix) method. We've created a File reference. Using createTempFile() method, we're creating the file in temporary 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); // 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: C:\Users\intel\AppData\Local\Temp\tmp2433347031187124180.tmp
Example - Usage of File createTempFile(String prefix, String suffix) method
The following example shows the usage of Java File createTempFile(String prefix, String suffix) method. We've created a File reference. Using createTempFile() method, we're creating the file in temporary 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"); // 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)