public class ManageContent { PRivate int dir_deep=0; private int dir_size = 0; private int sourcepath_deep; //輸入路徑相對(duì)于根目錄的深度 private Hashtable ht=new Hashtable(); //儲(chǔ)存輸入路徑下的所有子目錄,及其相對(duì)于根目錄的深度keys=(String)dirPath,values=(Integer)dirDeep
public synchronized int getSize(String dir_path) throws FileNotFoundException { File dir = new File(dir_path); if (!dir.exists()) { throw new FileNotFoundException(); } if (!dir.isDirectory()) { dir_size = (int) dir.length(); } else { File[] fe = dir.listFiles(); for (int i = 0; i < fe.length; i++) { if (fe[i].isDirectory()) { getSize(fe[i].toString()); } else { dir_size += fe[i].length(); } } } return dir_size; }
public synchronized void deleteFiles(String dir_path) throws FileNotFoundException { File file = new File(dir_path); if (!file.exists()) { throw new FileNotFoundException(); } if (file.isDirectory()) { File[] fe = file.listFiles(); for (int i = 0; i < fe.length; i++) { deleteFiles(fe[i].toString()); fe[i].delete(); //刪除已經(jīng)是空的子目錄 } } file.delete(); //刪除總目錄 }
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; //輸入路徑不是目錄時(shí)就返回-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; }