首页 > Windows开发 > 详细

windows文件夹嵌套太多,导致无法删除的解决方法

时间:2019-01-24 15:26:01      阅读:476      评论:0      收藏:0      [点我收藏+]
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文件夹嵌套太多,导致无法删除的解决方法

原文:https://www.cnblogs.com/jeremyyjy/p/10314668.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!