fix del bug

This commit is contained in:
Looly
2021-08-02 12:08:57 +08:00
parent e4848d5fe7
commit 69b981e8c0
3 changed files with 22 additions and 11 deletions

View File

@@ -710,10 +710,7 @@ public class FileUtil extends PathUtil {
// 删除文件或清空后的目录
try {
Files.delete(file.toPath());
} catch (AccessDeniedException access) {
// 可能遇到只读文件,无法删除.使用 file 方法删除
return file.delete();
delFile(file.toPath());
} catch (IOException e) {
throw new IORuntimeException(e);
}

View File

@@ -135,12 +135,7 @@ public class PathUtil {
if (isDirectory(path)) {
Files.walkFileTree(path, DelVisitor.INSTANCE);
} else {
try {
Files.delete(path);
} catch (AccessDeniedException access) {
// 可能遇到只读文件,无法删除.使用 file 方法删除
return path.toFile().delete();
}
delFile(path);
}
} catch (IOException e) {
throw new IORuntimeException(e);
@@ -596,4 +591,22 @@ public class PathUtil {
public static Path mkParentDirs(Path path) {
return mkdir(path.getParent());
}
/**
* 删除文件,不追踪软链
*
* @param path 文件对象
* @throws IORuntimeException IO异常
* @since 5.7.7
*/
protected static void delFile(Path path) throws IOException {
try {
Files.delete(path);
}catch (AccessDeniedException e) {
// 可能遇到只读文件,无法删除.使用 file 方法删除
if(false == path.toFile().delete()) {
throw e;
}
}
}
}