码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2019-01-24 15:35:32      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:void   str   tostring   嵌套   static   oid   file   rect   tab   

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文件夹嵌套太多,导致无法删除的解决方法

标签:void   str   tostring   嵌套   static   oid   file   rect   tab   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!