java类库的解释是这样的
Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification (12.9).
只有jvm正常退出才会消除临时文件。
比如
import java.io.*;
class foo{
public static void main(String []args) throws IOException{
File file = new File("e:\\exam\\");
File newfile = File.createTempFile("111",".xml",file);
newfile.deleteOnExit();
System.out.println(newfile);
while(true);
}
}
这样只有按ctrl+c和直接关闭DOS窗口叫非正常退出,这是不会删除文件。
如果把while(true);语句去处,就可以删除文件了。
Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification (12.9).
只有jvm正常退出才会消除临时文件。
比如
import java.io.*;
class foo{
public static void main(String []args) throws IOException{
File file = new File("e:\\exam\\");
File newfile = File.createTempFile("111",".xml",file);
newfile.deleteOnExit();
System.out.println(newfile);
while(true);
}
}
这样只有按ctrl+c和直接关闭DOS窗口叫非正常退出,这是不会删除文件。
如果把while(true);语句去处,就可以删除文件了。
还是不大懂
我用什么方法可以关闭它?难道我都关机了虚拟机也没关吗???
只有程序执行到末尾退出(例如把上面的while去掉),或者调用System.exit(0),才算正常退出。JVM收到这个指令,才有时间删除临时文件和真正退出。