如果我爲某些寫入操作創建臨時文件,有沒有什麼方法可以在程序中指定在程序終止時自動刪除它。程序終止時臨時文件刪除
下面是用於創建文件的過程。
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