windows文件夹嵌套太多,导致无法删除的解决方法
2021-06-18 07:04
标签:void str tostring 嵌套 static oid file rect tab windows文件夹嵌套太多,导致无法删除的解决方法 标签:void str tostring 嵌套 static oid file rect tab 原文地址:https://www.cnblogs.com/jeremyyjy/p/10314668.htmlimport java.io.File;
public class DeleteUtils
{
public static void main(String[] args)
{
File dir = new File("D:\\code\\NCE\\Spliter_NCE");
renameDirRecursively(dir);
System.out.println("renameDirRecursively success");
deleteRecursively(dir);
System.out.println("deleteRecursively success");
}
private static void renameDirRecursively(File dir)
{
File[] listFiles = dir.listFiles();
if (listFiles != null)
{
int subIndex = 0;
for (File file : listFiles)
{
boolean isDir = file.isDirectory();
StringBuilder afterName = new StringBuilder();
subIndex = renameFile(file, subIndex, afterName);
if (isDir)
{
renameDirRecursively(new File(afterName.toString()));
}
}
}
}
private static int renameFile(File file, int index, StringBuilder afterName)
{
String newName = file.getParent() + File.separator + index;
if (afterName != null)
{
afterName.append(newName);
}
if (newName.equals(file.getAbsolutePath()))
{
return index + 1;
}
boolean success = file.renameTo(new File(newName));
if (success)
{
return index + 1;
}
else
{
return renameFile(file, index + 1, afterName);
}
}
private static void deleteRecursively(File file)
{
if (file.isDirectory())
{
File[] listFiles = file.listFiles();
if (listFiles != null)
{
for (File subFile : listFiles)
{
deleteRecursively(subFile);
}
}
file.delete();
}
else
{
file.delete();
}
}
}
下一篇:c#泛型委托