Creating a temporary file in Java is straightforward. There are two static methods named createTempFile in the Java File class, one that takes two arguments, and another that takes three arguments. I created the test class shown below to demonstrate how to create a Java temporary file.
Here's the source code for temporary file example:
import java.io.File; import java.io.IOException; public class JavaTemporaryFiles { public static void main(String[] args) throws IOException { String prefix = "abc"; String suffix = ".tmp"; // this temporary file remains after the jvm exits File tempFile = File.createTempFile(prefix, suffix); System.out.format("Canonical filename: %s\n", tempFile.getCanonicalFile()); }} |