當開了像是 Reader、Writer、InputStream、OutputStream 等串流,最後要記得在 finally 去關閉,不然會導致串流一直吃著,之後有要對同一個檔案修改、刪除時,就會無法正常操作。
private static void close(Closeable obj) throws IOException {
if (obj != null) {
obj.close();
}
}
public boolean forceDelete(File file) {
boolean result = file.delete();
int tryCount = 0;
while (!result && tryCount++ < 10) {
System.gc(); //回收資源
result = file.delete();
}
return result;
}