Following example shows how to create temp file using file.createTempFile() method in Java.
createTempFile
public static File createTempFile(String prefix,String suffix) throws IOException Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. Invoking this method is equivalent to invoking createTempFile(prefix, suffix, null).
The Files.createTempFile method provides an alternative method to create an empty file in the temporary-file directory. Files created by that method may have more restrictive access permissions to files created by this method and so may be more suited to security-sensitive applications.
Parameters:
prefix - The prefix string to be used in generating the file's name; must be at least three characters long
suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
Returns:
An abstract pathname denoting a newly-created empty file
Throws:
IllegalArgumentException - If the prefix argument contains fewer than three characters
IOException - If a file could not be created
SecurityException - If a security manager exists and its
SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created
Sample Program
CreateTempFile.java
Result
The above code will create temp file and produce the following output.
createTempFile
public static File createTempFile(String prefix,String suffix) throws IOException Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. Invoking this method is equivalent to invoking createTempFile(prefix, suffix, null).
The Files.createTempFile method provides an alternative method to create an empty file in the temporary-file directory. Files created by that method may have more restrictive access permissions to files created by this method and so may be more suited to security-sensitive applications.
Parameters:
prefix - The prefix string to be used in generating the file's name; must be at least three characters long
suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
Returns:
An abstract pathname denoting a newly-created empty file
Throws:
IllegalArgumentException - If the prefix argument contains fewer than three characters
IOException - If a file could not be created
SecurityException - If a security manager exists and its
SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created
Sample Program
CreateTempFile.java
package com.javatutorialcorner.file; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class CreateTempFile { public static void main(String[] args) throws IOException { File tempFile = File.createTempFile("tempfile", ".txt"); tempFile.deleteOnExit(); BufferedWriter out = new BufferedWriter(new FileWriter(tempFile)); out.write("test temp file "); System.out.println("temporary file tempfile.txt created:"); out.close(); } }
Result
The above code will create temp file and produce the following output.
temporary file tempfile.txt created
0 comments:
Post a Comment