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.
虛擬機(jī)終止時(shí),此方法用于刪除文件或目錄。
The return type of this method is void so it does not return anything.
此方法的返回類型為void,因此它不返回任何內(nèi)容。
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.
在此方法中,以相反的順序刪除文件或目錄,這意味著在虛擬機(jī)終止時(shí),將首先刪除最后創(chuàng)建的文件或目錄。
This method may raise an exception (i.e. Security Exception) delete access is not given to the file.
此方法可能會(huì)引發(fā)異常(即Security Exception),但未授予該文件訪問權(quán)限。
Syntax:
句法:
void deleteOnExit(){}Parameter(s):
參數(shù):
We don't pass any object as a parameter in the method of the File.
我們不會(huì)在File方法中將任何對(duì)象作為參數(shù)傳遞。
Return value:
返回值:
The return type of this method is void, it does not return anything.
此方法的返回類型為void ,它不返回任何內(nèi)容。
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
總結(jié)
以上是生活随笔為你收集整理的Java File类void deleteOnExit()方法(带示例)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java StringBuilder c
- 下一篇: java 方法 示例_Java集合syn