windows文件夹嵌套太多,导致无法删除的解决方法
Posted jeremyyjy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows文件夹嵌套太多,导致无法删除的解决方法相关的知识,希望对你有一定的参考价值。
import 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(); } } }
以上是关于windows文件夹嵌套太多,导致无法删除的解决方法的主要内容,如果未能解决你的问题,请参考以下文章