Java 遍历目录实现删除,取得大小及深度

 

Java 遍历目录实现删除,取得大小及深度
出处:CSDN 2004年06月29日 作者:shuqianlz 责任编辑:linjixiong

文章导读:  以下的类就实现了对目录的操作,包括删除,取大小以及深度。 import java.io.*;import java.util.*;public class

ManageContent {  private int dir_deep=0;  private int dir_size = 0;  private int sourcepath_dee

 


   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;
  }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值