zoukankan      html  css  js  c++  java
  • 删除电脑里的空文件夹

    发现电脑好多空文件夹,网上没有找到删除空文件夹的程序,自己动手谢了段代码

    import java.io.File;
    
    /**
     * @author zhangyanan
     */
    public class FileUtil { 
        public static void main(String args[]) {
            String[] disk={"C:/","D:/","E:/","F:/","G:/","H:/"};//具体磁盘数组
            for(int i=0;i<disk.length;i++){
                deleteEmptyFolder(new File(disk[i]));//循环删除各个磁盘空文件夹
            }
        }
        
        /**
         * @todo 删除空文件夹
         * @author zhangyanan
         * @datetime 2018年1月13日上午10:15:59
         * @param path
         */
        public static void deleteEmptyFolder(File path) {
            
            File[] files = path.listFiles();
            if (files==null||files.length == 0) {//空文件夹就删除.
                System.out.println(path.getPath()+"	"+path.getName());
                path.delete();//没有权限的文件夹不会删除;。安全起见这里不探讨删除没有权限的空文件夹
                // deleteDirectory(path);
            } else {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        deleteEmptyFolder(files[i]);
                    }
                }
            }
    
        }
    }
    View Code

     20190918:优化了一下

    import java.io.File;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * @author zhangyanan
     */
    public class FileUtil { 
        private static List<String> list=new LinkedList<String>();//没删除成功的文件夹,跳过
         public static void main(String args[]) {
            String[] disk={"C:/","D:/","E:/","F:/","G:/","H:/"};//具体磁盘数组
             for(int i=0;i<disk.length;i++){
                deleteEmptyFolder(new File(disk[i]));//循环删除各个磁盘空文件夹
            }
        }
        
        /**
         * @todo 删除空文件夹
         * @author zhangyanan
         * @datetime 2018年1月13日上午10:15:59
         * @param path
         */
        public static void deleteEmptyFolder(File path) {
            if(path==null){
                return;
            }
            File[] files = path.listFiles();
            //受保护的文件夹是null,不是保护的空文件夹length=0
            if(files==null){//受保护的文件不处理
                return;
            }
            if (files.length == 0) {//空文件夹就删除.
                String parent = path.getParent();
    
                boolean delete = path.delete();//没有权限的文件夹不会删除;。安全起见这里不探讨删除没有权限的空文件夹
                if(!delete){
                    list.add(path.getPath());
                }else{
                    System.out.println("------------------------------delete path	"+path.getPath()+"	"+"	parentPath	"+parent);
                }
                if(parent!=null){
                    deleteEmptyFolder(path.getParentFile()); 
                }
                
            } else {
               // System.out.println(files.length+"	"+path.getPath());
                if(files.length>100){//文件夹太多的就不处理了,空文件夹的可能性不大
                    return;
                }
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()&&!files[i].isHidden()&&!list.contains(files[i].getPath())) {
                        deleteEmptyFolder(files[i]);
                    }
                }
            }
            
    
        }
    }
    View Code
  • 相关阅读:
    Analysis Services features supported by SQL Server editions
    Azure DevOps to Azure AppServices
    Power BI For Competition
    Win10开机“提示语音”以及”随机播放音乐”
    Azure DevOps
    Allow Only Ajax Requests For An Action In ASP.NET Core
    Mobile CI/CD 101
    Configure SSL for SharePoint 2013
    AWS Step Function Serverless Applications
    Cordova Upload Images using File Transfer Plugin and .Net core WebAPI
  • 原文地址:https://www.cnblogs.com/yanan7890/p/8278472.html
Copyright © 2011-2022 走看看