This commit is contained in:
Looly 2021-08-20 16:30:31 +08:00
parent 6a3639a78e
commit d52c32154a
3 changed files with 16 additions and 5 deletions

View File

@ -709,8 +709,12 @@ public class FileUtil extends PathUtil {
} }
// 删除文件或清空后的目录 // 删除文件或清空后的目录
final Path path = file.toPath();
try { try {
delFile(file.toPath()); delFile(path);
} catch (DirectoryNotEmptyException e){
// 遍历清空目录没有成功此时补充删除一次可能存在部分软链
del(path);
} catch (IOException e) { } catch (IOException e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
} }
@ -749,10 +753,8 @@ public class FileUtil extends PathUtil {
final File[] files = directory.listFiles(); final File[] files = directory.listFiles();
if (null != files) { if (null != files) {
boolean isOk;
for (File childFile : files) { for (File childFile : files) {
isOk = del(childFile); if(false == del(childFile)){
if (isOk == false) {
// 删除一个出错则本次删除任务失败 // 删除一个出错则本次删除任务失败
return false; return false;
} }

View File

@ -644,7 +644,7 @@ public class PathUtil {
} }
/** /**
* 删除文件不追踪软链 * 删除文件或空目录不追踪软链
* *
* @param path 文件对象 * @param path 文件对象
* @throws IOException IO异常 * @throws IOException IO异常

View File

@ -23,6 +23,15 @@ public class DelVisitor extends SimpleFileVisitor<Path> {
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
/**
* 访问目录结束后删除目录当执行此方法时子文件或目录都已访问删除完毕<br>
* 理论上当执行到此方法时目录下已经被清空了
*
* @param dir 目录
* @param e 异常
* @return {@link FileVisitResult}
* @throws IOException IO异常
*/
@Override @Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
if (e == null) { if (e == null) {