Sometimes you may want to create temporary file in Java to store some data for the application once the job is done temp file can safely be discarded or you can delete it on exit.
In java.io.File class there are two methods for creating a temp file.
- static File createTempFile(String prefix, String suffix, File directory)- Creates a new empty file in the specified directory, using the passed prefix and suffix strings to generate file name.
- static File createTempFile(String prefix, String suffix)- Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
In java.nio.file.Files class also there are two methods for creating temporary files.
- static Path createTempFile(Path dir, String prefix, String suffix, FileAttribute<?>... attrs)- Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. You can also pass an optional list of file attributes to set atomically when creating the file
- static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs)- Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
Creating temporary file using java.io.File class methods
import java.io.File;
import java.io.IOException;
public class TempFile {
public static void main(String[] args) {
try {
// Using default directory
File tempFile = File.createTempFile("MyFile", ".temp");
System.out.println("Temp file path- " + tempFile.getCanonicalPath());
// Specifying directory
File testFile = File.createTempFile("MyFile", ".temp", new File("F:\\Temp"));
System.out.println("Temp file path- " + testFile.getCanonicalPath());
tempFile.deleteOnExit();
testFile.deleteOnExit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output
Temp file path- C:\Users\netjs\AppData\Local\Temp\MyFile13999667283141733746.temp
Temp file path- F:\Temp\MyFile11668072031090823667.temp
Creating temporary file using java.nio.file.Files class methods
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class TempFile {
public static void main(String[] args) {
try {
// Using default directory
Path tempFile = Files.createTempFile("MyFile", ".temp");
System.out.println("Temp file path- " + tempFile);
// Specifying directory
Path testFile = Files.createTempFile(Path.of("F:\\Temp"), "MyFile", ".temp");
System.out.println("Temp file path- " + testFile);
// Read write temp file operations
// delete file on exit
tempFile.toFile().deleteOnExit();
testFile.toFile().deleteOnExit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output
Temp file path- C:\Users\Anshu\AppData\Local\Temp\MyFile2647482374773079441.temp
Temp file path- F:\Temp\MyFile16822359990880283479.temp
That's all for this topic Creating Temporary File in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Advanced Tutorial Page
Related Topics
You may also like -