import java.io.*;
class  DeleteDemo
{
public static void main(String[] args) throws Exception
{
File f=new File("c:\\test.txt");
f.createNewFile();
for(int x=0; x<50; x++)
{
System.out.println("Hello World!"+x);
}
f.deleteOnExit();
}
}
各位,为何程序运行在c盘看到test.txt文件的建立?可是把x<50000就可以看到文件的建立以及循环结束后文件被自动删除。希望有专业的解释。谢谢。另,deleteOnExit()和delete()有什么区别?请详细解释一下deleteOnExit()方法的用法。在这里先谢谢了。

解决方案 »

  1.   

    deleteOnExit
    public void deleteOnExit()
    Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification. 
    Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care.