private void allDeep(File file){ File[] fe = file.listFiles(); for (int i = 0; i < fe.length; i++) { if (fe[i].isDirectory()) { allDeep(fe[i]); //取得其所有子目录的深度 ht.put(fe[i].toString(),new Integer(count(fe[i].toString()) - sourcepath_deep)); } } } public synchronized int getDeep(String dir_path) throws FileNotFoundException{ File file = new File(dir_path); if (!file.exists()) { throw new FileNotFoundException(); } if(!file.isDirectory()){ return -1; //输入路径不是目录时就返回-1 } sourcepath_deep=count(file.toString()); allDeep(file); Enumeration enum=ht.keys(); int max=0; String keys; Integer values; while(enum.hasMoreElements()){ keys=(String)enum.nextElement(); values=(Integer)ht.get(keys); if(values.intValue()>max) max=values.intValue(); } return max; } private int count(String str){ String regEx="/"; if(System.getProperty("file.separator").equals("\\")) //windows和unix下的路径分割符不一样 regEx="\\\\"; return str.split(regEx).length; } } |