zoukankan      html  css  js  c++  java
  • 删除指定目录下的空文件夹

    //删除制定目录下的所有空文件夹
    //num=0 第一次遍历该文件夹 num=1 第二次遍历该文件夹
    public static void ListDirectory(File file,int num)throws Exception
    	{
    		if(!file.exists())
    		{
    			throw new IllegalArgumentException("目录"+file+"不存在");
    		}
    		else if(!file.isDirectory())
    		{
    			throw new IllegalArgumentException(file+"不是目录");
    		}
    		else {//进入目录
    			
    			File[] fl = file.listFiles();
    			if(fl!=null && fl.length>0)
    			{
    				if(num==1) return ;
    				for(File f:fl)
    				{
    					if(f.isDirectory())
    					{
    						ListDirectory(f,0);
    						
    					}
    					else {
    						System.out.println(f.getName());
    					}
    				}
    				ListDirectory(file,1);//在目录遍历完成之后再遍历下目录下是否有文件
    			}
    			else if(fl == null || fl.length==0)
    			{
    				System.out.println(file.getName()+":目录为空");
    				file.delete();
    				
    				
    			}								
    		}
    	}
    

      

  • 相关阅读:
    异常处理
    组合,封装
    自我介绍
    27python更多实例
    28python类代码编写细节
    29python运算符重载
    30python 类的设计
    31python类的高级主题
    32python异常基础
    33python异常编码细节
  • 原文地址:https://www.cnblogs.com/lobsterIT/p/4907359.html
Copyright © 2011-2022 走看看