android删除文件出错

it2025-07-11  5

当删除一个文件,再又一次下载这个同名文件,保存到sdcard时出现error,部分手机出现

Caused by: libcore.io.ErrnoException: open failed: EBUSY (Device or resource busy) at libcore.io.Posix.open(Native Method) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) at java.io.File.createNewFile(File.java:941)

此问题在小米3。华为系列手机出现概率较大。 文件创建失败的原因是。文件被删除后仍然被其它进程占用。 进入adb shell。通过lsof命令查看占用该文件的进程。 据说这是android文件系统的bug,建议删除文件前先将该文件进行重命名:

删除文件安全方式:

 private void deleteFile(File file) {         if (file.isFile()) {             deleteFileSafely(file);             return;         }         if (file.isDirectory()) {             File[] childFiles = file.listFiles();             if (childFiles == null || childFiles.length == 0) {                 deleteFileSafely(file);                 return;             }             for (int i = 0; i < childFiles.length; i++) {                 deleteFile(childFiles[i]);             }             deleteFileSafely(file);         }     }

    /**      * 安全删除文件.      * @param file      * @return      */     public static boolean deleteFileSafely(File file) {         if (file != null) {             String tmpPath = file.getParent() + File.separator + System.currentTimeMillis();             File tmp = new File(tmpPath);             file.renameTo(tmp);             return tmp.delete();         }         return false;     }



转载于:https://www.cnblogs.com/bhlsheji/p/5081452.html

相关资源:数据结构—成绩单生成器
最新回复(0)