import java.io.File; import java.io.IOException; public class TempFileExample { public static void main(String[] args) throws IOException { File tempFile = File.createTempFile("mytempfile", ".txt"); System.out.println("Created temporary file: " + tempFile.getAbsolutePath()); } }
import java.io.File; import java.io.IOException; public class TempFileExample { public static void main(String[] args) throws IOException { File tempFile = File.createTempFile("mytempfile", ".txt", new File("/tmp")); System.out.println("Created temporary file: " + tempFile.getAbsolutePath()); } }In this example, a temporary file with the prefix "mytempfile" and the suffix ".txt" is created in the "/tmp" directory. The file object is then printed to the console. The createTempFile() method is part of the java.util package library.