岁月联盟 · 中国技术网 本站主页 | 安全认证 | 用户服务 | 技术论坛
新闻快报 | 新手学堂 | 黑客特区 | 程序语言 | 数 据 库 | 防 火 墙 | 路由交换 | 系统集成 | 服 务 器 | 存储备份 | 考试认证
Windows | Linux | Java | 协议分析 | 问题解答 | 进程大全 | 网页设计 | 多 媒 体 | 图库资料 | 软件下载 | 站内下载
  您现在的位置: 岁月联盟 >> 新手学堂 >> 新手学堂 >> 应用技巧 >> 文章正文
Java路径问题最终解决方案
作者:未知 文章来源:本站整理 点击数: 更新时间:2006-12-7 18:43:20
on
       *@throwsIOException
       */
     publicstatic InputStream getStreamByExtendResource(String relativePath) throws MalformedURLException, IOException{
        return ClassLoaderUtil.getStream(ClassLoaderUtil.getExtendResource(relativePath));
        
        
     }
    
      /**
       *提供相对于classpath的资源路径,返回属性对象,它是一个散列表
       *@paramresource
       *@return
       */
     publicstatic Properties getProperties(String resource) {
        Properties properties = new Properties();
        try {
          properties.load(getStream(resource));
        } catch (IOException e) {
          thrownew RuntimeException("couldn’t load properties file ’"+resource+"’", e);
        }
        return properties;
     }
     /**
       *得到本Class所在的ClassLoader的Classpat的绝对路径。
       *URL形式的
       *@return
       */
     publicstatic String getAbsolutePathOfClassLoaderClassPath(){
        
        
         ClassLoaderUtil.log.info(ClassLoaderUtil.getClassLoader().getResource("").toString());
         return ClassLoaderUtil.getClassLoader().getResource("").toString();
        
     }

/**
       *
       *@paramrelativePath 必须传递资源的相对路径。是相对于classpath的路径。如果需要查找classpath外部的资源,需要使用../来查找
       *@return资源的绝对URL
     *@throwsMalformedURLException
       */
     publicstatic URL getExtendResource(String relativePath) throws MalformedURLException{
    
         ClassLoaderUtil.log.info("传入的相对路径:"+relativePath) ;
         //ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf("../"))) ;
         if(!relativePath.contains("../")){
             return ClassLoaderUtil.getResource(relativePath);
            
         }
         String classPathAbsolutePath=ClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath();
         if(relativePath.substring(0, 1).equals("/")){
             relativePath=relativePath.substring(1);
         }
         ClassLoaderUtil.log.info(Integer.valueOf(relativePath.lastIndexOf("../"))) ;
       
         String wildcardString=relativePath.substring(0,relativePath.lastIndexOf("../")+3);
        relativePath=relativePath.substring(relativePath.lastIndexOf("../")+3);
         int containSum=ClassLoaderUtil.containSum(wildcardString, "../");
         classPathAbsolutePath= ClassLoaderUtil.cutLastString(classPathAbsolutePath, "/", containSum);
         String resourceAbsolutePath=classPathAbsolutePath+relativePath;
         ClassLoaderUtil.log.info("绝对路径:"+resourceAbsolutePath) ;
         URL resourceAbsoluteURL=new URL(resourceAbsolutePath);
         return resourceAbsoluteURL;
     }
     /**
      *
       *@paramsource
       *@paramdest
       *@return
       */
     privatestaticint containSum(String source,String dest){
         int containSum=0;
         int destLength=dest.length();
         while(source.contains(dest)){
             containSum=containSum+1;
             source=source.substring(destLength);
            
         }
        
        
         return containSum;
     }
     /**
       *
       *@paramsource
       *@paramdest
       *@paramnum
       *@return
       */
     privatestatic String cutLastString(String source,String dest,int num){
         // String cutSource=null;
         for(int i=0;i<num;i++){
             source=source.substring(0, source.lastIndexOf(dest, source.length()-2)+1);
            
            
         }
        
        
        
         return source;
     }
     /**
       *
       *@paramresource
       *@return
       */
      publicstatic URL getResource(String resource){
      ClassLoaderUtil.log.info("传入的相对于classpath的路径:"+resource) ;
         return ClassLoaderUtil.getClassLoader().getResource(resource);
     }
    
 
    
 
    /**
     *@paramargs
     *@throwsMalformedURLException
     */
    publicstaticvoid main(String[] args) throws MalformedURLException {
       
            //ClassLoaderUtil.getExtendResource("../spring/dao.xml");
        //ClassLoaderUtil.getExtendResource("../../../src/log4j.properties");
        ClassLoaderUtil.getExtendResource("log4j.properties");
       
        System.out.println(ClassLoaderUtil.getClassLoader().getResource("log4j.properties").toString());
 
    }
 
}
 
  后记

  ClassLoaderUtil类的public static URL getExtendResource(String relativePath),虽然很简单,但是确实可以解决大问题。

  不过这个方法还是比较简陋的。我还想在未来有空时,进一步增强它的能力。比如,增加Ant风格的匹配符。用**代表多个目录,*代表多个字符,?代表一个字符。达到Spring那样的能力,一次返回多个资源的URL,进一步方便大家开发。
 
  总结:

  1,尽量不要使用相对于System.getProperty("user.dir")当前用户目录的相对路径。这是一颗定时炸弹,随时可能要你的命。

  2,尽量使用URI形式的绝对路径资源。它可以很容易的转变为URI,URL,File对象。

  3,尽量使用相对classpath的相对路径。不要使用绝对路径。使用上面ClassLoaderUtil类的public static URL getExtendResource(String relativePath)方法已经能够使用相对于classpath的相对路径定位所有位置的资源。

  4,绝对不要使用硬编码的绝对路径。因为,我们完全可以使用ClassLoader类的getResource("")方法得到当前classpath的绝对路径。

  使用硬编码的绝对路径是完全没有必要的!它一定

上一页  [1] [2] [3] 下一页


  • 上一篇文章:
  • 下一篇文章:
  •  
    热门文章
    推荐文章
    关于我们 | 发展历程 | 网站地图 | 广告服务 | 招贤纳士 | 战略合作 | 友情链接 | 著作声明 | 联系我们
    Copyright © 2002-2007 SYUE All rights reserved.
    E_mail:Admin@Syue.Com 皖ICP备05004589号
    未经授权禁止转载、摘编、复制或建立镜像.如有违反,追究法律责任.
    bet365 传世私服 传奇世界私服 bet365 传奇私服 bet365 热血江湖私服 天龙八部私服