现在的位置: 首页 > 综合 > 正文

RuntimeException一次性中断退出递归函数

2018年05月09日 ⁄ 综合 ⁄ 共 634字 ⁄ 字号 评论关闭
/**
	 * 递归查找文件
	 * @param basePath
	 * @param methodName
	 * @return
	 */
	private String Recurisive(String basePath, String methodName)
	{  
        // TODO Auto-generated method stub  
        File file = new File(basePath);  
          
        File[] files = file.listFiles();  
        if(files==null)
		{    
			return result;
		} 
        String fileName = null;
        
        for (int i = 0; i < files.length; i++) { 
        	fileName = files[i].getName();
            if(files[i].isDirectory()==true)
            { 
                Recurisive(basePath+fileName+"/",methodName); 
            }else{
            	result = searchMethod(basePath,methodName,fileName);
                if(!result.equals(UN_USER))
                {
                	/*当搜索到关键字,则通过异常一次性跳出递归*/
                	throw new StopRecException();
                }
            }
        }
        
        return result;
    }

	static class StopRecException extends RuntimeException 
	{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
    }

【上篇】
【下篇】

抱歉!评论已关闭.