Java File类void deleteOnExit()方法(带示例)
文件類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 fileString 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 terminatesfile1.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.javaE:\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
總結
以上是生活随笔為你收集整理的Java File类void deleteOnExit()方法(带示例)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java StringBuilder c
- 下一篇: 重疾险一年交多少钱啊?