字符串路径比较

import java.io.*;
public class Test {
public static void main(String args[]){
    File file=new File("C:\\Users\\Administrator\\Desktop\\123");
    System.out.println("指定路径为: "+file.getName());
    String path="C:\\Users\\Administrator\\Desktop\\123\\ed";
    CharSequence path1= path;
    String ppath=findDir(file,path1);
    System.out.println(ppath);
}

public static String findDir(File file,CharSequence path1){
    //如果路径可以使用那么判断路径下的文件夹
    if(file.exists()){
        File[] files=file.listFiles();
            for(int i=0;i<files.length;i++)
            {
                if(files[i].isDirectory()){
                    //如果发现是文件夹那么,判断获取其路径信息并且进行判断
                    String temp=null;
                    try {
                        //获得当前文件夹的路径
                        temp = files[i].getCanonicalPath();
                        //System.out.println("文件夹路径为:"+files[i].getCanonicalPath());
                        //temp=temp.replace("\\","\\\\");
                        //System.out.println("path1:"+path1);
                           if(temp.contains(path1)){
                               System.out.println("!!!!!!!");
                                return temp;
                           }
                    
                        
                    } catch (IOException e) {
                    
                    }
                }
            }
        }
    return null;
        
    }

}



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

需要传入的是一个路径


路径是  原始的我们定义的path  也就是logs\log123456

因此在ant扩展中

private String comparePath;  (我们要拿的path)这个每次是不一样的  根据lastbudild来
private String path;          (也就是上一级路径这个可以写死)
private String propertyName;  (这个是处理后需要返回的值)


//针对  comparePath 和 path 直接生成   setter   getter

  public void setProperty(String propertyName) {
        this.propertyName = propertyName;
    }
    
    
/* public void execute(){
    
    //这里实际上是给  property赋值的也就是传回的
     getProject().setNewProperty(this.propertyName, "test change value in own task");
} */
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
注意生成ant扩展包,  wc目录下    build.xml  修改两部分  一个是exclude   一个是单独的   dest
生成后导入到相应的路径下  

最后修改一下  将所有依赖包放置到   tool中    

转侧文档  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<GetPath
  property="getpath"   //要传回来的值
  path="";//这一级别到log
  comparePath="${propArg}"//这一级到  lastbuild
 />
 
 <echo message="------------------- ${getpath} -------------------------------------" />
 
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

在扩展里这样写,注意getter   setter  就可以了
http://www.oschina.net/question/234345_51227?fromerr=1Ho5lT7l
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        
public class  GetPath{
        private String comparePath;  (我们要拿的path)这个每次是不一样的  根据lastbudild来
        private String path;          (也就是上一级路径这个可以写死)
        private String propertyName;  (这个是处理后需要返回的值)
        //生成setter   getter
        
     public void setProperty(String propertyName) {
        this.propertyName = propertyName;
    }
    
public  void execute(){
    File file=new File(path);
    System.out.println("指定路径为: "+file.getName());
    String path=comparePath;
    CharSequence path1= path;
    String returnPath=findDir(file,path1);
    getProject().setNewProperty(this.propertyName, returnPath);//将returnPath的值传回到ant 插件中
    System.out.println("返回的路径:"+returnPath);
}

public  String findDir(File file,CharSequence path1){
    //如果路径可以使用那么判断路径下的文件夹
    if(file.exists()){
        File[] files=file.listFiles();
            for(int i=0;i<files.length;i++)
            {
                if(files[i].isDirectory()){
                    //如果发现是文件夹那么,判断获取其路径信息并且进行判断
                    String temp=null;
                    try {
                        //获得当前文件夹的路径
                        temp = files[i].getCanonicalPath();
                        //System.out.println("文件夹路径为:"+files[i].getCanonicalPath());
                        //temp=temp.replace("\\","\\\\");
                        //System.out.println("path1:"+path1);
                           if(temp.contains(path1)){
                               System.out.println("!!!!!!!");
                                return temp;
                           }
                    } catch (IOException e) {
                    
                    }
                }
            }
        }
    return null;
        
    }
}
里面的处理是自动获取当前路径下的文件夹

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值