以下实例演示了使用 File 类的 createTempFile() 方法来创建临时文件:
/* author by shouce.ren Main.java */ import java.io.*; public class Main { public static void main(String[] args) throws Exception { File temp = File.createTempFile ("pattern", ".suffix"); temp.deleteOnExit(); BufferedWriter out = new BufferedWriter (new FileWriter(temp)); out.write("aString"); System.out.println("临时文件已创建:"); out.close(); } }
以上代码运行输出结果为:
临时文件已创建: