国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

實現目錄刪除,取得目錄大小及深度

2019-11-18 13:16:14
字體:
來源:轉載
供稿:網友

  import java.io.*;
  import java.util.*;
  
  public class ManageContent {
   PRivate int dir_deep=0;
   private int dir_size = 0;
   private int sourcepath_deep; //輸入路徑相對于根目錄的深度
   private Hashtable ht=new Hashtable(); //儲存輸入路徑下的所有子目錄,及其相對于根目錄的深度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(); //刪除已經是空的子目錄
     }
    }
    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; //輸入路徑不是目錄時就返回-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("http://")) //windows和unix下的路徑分割符不一樣
      regEx="http:////";
    return str.split(regEx).length;
   }
  }
  以上的類就實現了對目錄的操作,包括刪除,取大小以及深度。
  
  其中在取目錄深度時,我是通過分割字符串來求得其所有子目錄相對于根目錄的深度,然后再減去輸入目錄相對于根目錄的深度,就得到了輸入目錄的深度。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金山区| 万年县| 烟台市| 武威市| 和硕县| 景谷| 武夷山市| 南乐县| 莱阳市| 安阳市| 阿坝| 措美县| 噶尔县| 盐山县| 三门县| 古蔺县| 延川县| 康定县| 奉新县| 泽州县| 连州市| 清水河县| 安徽省| 宁都县| 安康市| 呈贡县| 鄂托克前旗| 台前县| 资源县| 安康市| 英德市| 宁国市| 于都县| 兴和县| 鹤峰县| 沿河| 自贡市| 清原| 德州市| 进贤县| 新野县|