文件类void deleteOnExit() (File Class void deleteOnExit())
- This method is available in package java.io.File.deleteOnExit(). - 软件包java.io.File.deleteOnExit()中提供了此方法。 
- This method is used to delete the file or directory when the virtual machine terminates. - 虚拟机终止时,此方法用于删除文件或目录。 
- The return type of this method is void so it does not return anything. - 此方法的返回类型为void,因此它不返回任何内容。 
- In this method delete file or directories in the reverse order, that means last created file or directories will be deleted first when virtual machine terminates. - 在此方法中,以相反的顺序删除文件或目录,这意味着在虚拟机终止时,将首先删除最后创建的文件或目录。 
- This method may raise an exception (i.e. Security Exception) delete access is not given to the file. - 此方法可能会引发异常(即Security Exception),但未授予该文件访问权限。 
Syntax:
句法:
    void deleteOnExit(){
}
Parameter(s):
参数:
We don't pass any object as a parameter in the method of the File.
我们不会在File方法中将任何对象作为参数传递。
Return value:
返回值:
The return type of this method is void, it does not return anything.
此方法的返回类型为void ,它不返回任何内容。
Java程序,演示deleteOnExit()方法的示例 (Java program to demonstrate example of deleteOnExit() method)
// import the File class because we will use File class methods
import java.io.File;
// import the Exception class because it may raise an 
// exception when working with files
import java.lang.Exception;
class DeleteFileOnExit {
public static void main(String[] args) {
try {
// Specify the path of file and we use double slashes to 
// escape '\' character sequence for windows otherwise 
// it will be considerable as url.
File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
// By using getAbsolutePath() return the complete 
// path of the file
String abs_path = file1.getAbsolutePath();
// Display absolute path of the file object 
System.out.println("The absolute path of the file 1 if given path is absolute :" + " " + abs_path);
// By using deleteOnExit() method to delete the file 
// when the virtual machine terminates
file1.deleteOnExit();
System.out.println("This file will delete as soon as the virtual machine terminates");
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output
输出量
E:\Programs>javac DeleteFileOnExit.java
E:\Programs>java DeleteFileOnExit
The absolute path of the file 1 if given path is absolute : C:\Users\computer clinic\OneDrive\Articles\myjava.txt
This file will delete as soon as the virtual machine terminates
翻译自: https://www.includehelp.com/java/file-class-void-deleteonexit-method-with-example.aspx