今天遇到一個奇怪的問題,
測試在程序的下載界面,下載一個文件第一次下載成功,刪除后再下載結果下載報錯,
程序:file.createNewFile();
報錯:java.io.IOException: open failed: EBUSY (Device or resource busy)
程序:RandomAccessFile randomAccessFile = new RandomAccessFile(localfile, "rwd");
報錯:java.io.FileNotFoundException: open failed: EBUSY (Device or resource busy)
at libcore.io.IoBridge.open
......一啪啦
這些手機都沒問題,就只有華為的一款有這個問題,在網上查了,說的是FAT32 system,
這是這個測試機的系統,具體的不知道跟這個到底有沒關系,反正根據網上一段話說的是把要刪除的文件改名然后再刪除,下次下載時,就算沒刪掉,文件名也改了,所以第二次就能下載了。
刪除文件代碼:
public void deleteSDCardFolder(File dir) {
File to = new File(dir.getAbsolutePath() + System.currentTimeMillis());
dir.renameTo(to);
if (to.isDirectory()) {
String[] children = to.list();
for (int i = 0; i < children.length; i++) {
File temp = new File(to, children[i]);
if (temp.isDirectory()) {
deleteSDCardFolder(temp);
} else {
boolean b = temp.delete();
if (b == false) {
Log.d("deleteSDCardFolder", "DELETE FAIL");
}
}
}
to.delete();
}
解決問題參考網址:http://stackoverflow.com/questions/11539657/open-failed-ebusy-device-or-resource-busy