1、file.delete刪除失敗,查詢資料都是說IO流未關閉,但是關閉也還是刪除失敗。手動刪除顯示該文件進程被占用,無法刪除。
/**
* 刪除單個文件
*
* @param fileName
* 要刪除的文件的文件名
* @return 單個文件刪除成功返回true,否則返回false
*/
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
// 如果文件路徑所對應的文件存在,並且是一個文件,則直接刪除
if (file.exists() && file.isFile()) {
if (file.delete()) {
log.info("刪除單個文件" + fileName + "成功!");
return true;
} else {
log.info("刪除單個文件" + fileName + "失敗!");
return false;
}
} else {
log.info("刪除單個文件失敗:" + fileName + "不存在!");
return false;
}
}
/**
* 強制刪除
* @param fileName
* @return
*/
public static boolean forceDelete(String fileName)
{
File f = new File(fileName);
boolean result = false;
int tryCount = 0;
while(!result && tryCount++ <10)
{
log.info("try to delete file "+ f.getName() +" cnt:"+tryCount);
System.gc();
result = f.delete();
}
return result;
}
————————————————
版權聲明:本文為CSDN博主「加州小熊」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/akemt/article/details/86488454
