例子
以下示例显示java.io.File.deleteOnExit()方法的用法。
package com.jc2182;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
try {
// creates temporary file
f = File.createTempFile("tmp", ".txt");
// prints absolute path
System.out.println("File path: "+f.getAbsolutePath());
// deletes file when the virtual machine terminate
f.deleteOnExit();
// creates temporary file
f = File.createTempFile("tmp", null);
// prints absolute path
System.out.print("File path: "+f.getAbsolutePath());
// deletes file when the virtual machine terminate
f.deleteOnExit();
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
File path: C:\Users\BABAI\AppData\Local\Temp\tmp1307307616656986083.txt
File path: C:\Users\BABAI\AppData\Local\Temp\tmp4586112702019401940.tmp