如果我为某些写入操作创建临时文件,有没有什么方法可以在程序中指定在程序终止时自动删除它。程序终止时临时文件删除
下面是用于创建文件的过程。
File tmp = File.createTempFile("foo", "tmp");
System.out.println("Your temp file is " + tmp.getCanonicalPath());
//doing something with the temporary file
writeDataInTemp(tmp.getCanonicalPath());
你使用任何框架或容器来运行你的代码?通常,框架和容器提供了一些关闭钩子,使用它可以在程序终止之前询问容器该做什么。在普通的Java中,你将不得不在'@ PreDestroy'方法中编写删除代码 – Prashant
没有使用框架。你所说的删除代码是一些现有的函数? – Aajan
在Predestroy示例中,我可以看到几种方法:http://www.programcreek.com/java-api-examples/javax.annotation.PreDestroy&ei=ZxjNmOrN&lc=en-IN&s=1&m=884&host=www.google.co.in&ts= 1457150499&sig = ALL1Aj7uy_LtEkUDTRqGX1wXWZ-mXKvzLw – Aajan