Java File deleteOnExit()方法

java.io.File.deleteOnExit() 方法删除由抽象路径名所定义的虚拟机终止时的文件或目录。文件或目录中,因为它们将按登记相反的顺序删除。

1 语法

public void deleteOnExit()

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.File.deleteOnExit()方法的例子
 */
import java.io.File;

public class Demo {
    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\lenovo\AppData\Local\Temp\tmp4375723762116872703.txt
File path: C:\Users\lenovo\AppData\Local\Temp\tmp2157298973091394685.tmp

热门文章

优秀文章