Java.io.File.deleteOnExit() 方法
描述
java.io.File.deleteOnExit() 方法在虚拟机终止时删除抽象路径名定义的文件或目录。 文件或目录的删除顺序与注册时相反。
声明
以下是 java.io.File.deleteOnExit() 方法的声明 −
public void deleteOnExit()
参数
NA
返回值
该方法不返回任何值。
异常
SecurityException − 如果 SecurityManager.checkWrite(java.lang.String) 方法拒绝对文件的删除访问
示例
下面的例子展示了 java.io.File.deleteOnExit() 方法的使用。
package com.tutorialspoint;
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

