日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

java路径解析

發布時間:2023/12/15 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 java路径解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java路徑最終解決方案http://blog.csdn.net/shendl/article/details/1427475

貼出作者寫的一個工具類

這個工具的思路大概就是:

如果路徑中沒有帶有../ 即路徑在classes中,那么可以直接調用下面的這行代碼獲取輸入流

如果路徑中帶有../那么作者就會將這個相對路徑進行解析,解析出其絕對路徑URL,然后通過URL.openStream()得到這個流

  1 package com.Utils;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.net.MalformedURLException;
  6 import java.net.URL;
  7 import java.util.Properties;
  8  
  9 import org.apache.commons.logging.Log;
 10 import org.apache.commons.logging.LogFactory;
 11  
 12 /**
 13  *@author沈東良shendl_s@hotmail.com
 14  *Nov29,2006 10:34:34AM
 15  *用來加載類,classpath下的資源文件,屬性文件等。
 16  *getExtendResource(StringrelativePath)方法,可以使用../符號來加載classpath外部的資源。
 17  */
 18 public class ClassLoaderUtil {
 19     private static Log log=LogFactory.getLog(ClassLoaderUtil.class);
 20     /**
 21      *Thread.currentThread().getContextClassLoader().getResource("")
 22      */
 23    
 24     /**
 25      *加載Java類。 使用全限定類名
 26      *@paramclassName
 27      *@return
 28      */
 29     public static Class loadClass(String className) {
 30         try {
 31           return getClassLoader().loadClass(className);
 32         } catch (ClassNotFoundException e) {
 33           throw new RuntimeException("class not found '"+className+"'", e);
 34         }
 35      }
 36      /**
 37        *得到類加載器
 38        *@return
 39        */
 40      public static ClassLoader getClassLoader() {
 41      
 42         return ClassLoaderUtil.class.getClassLoader();
 43      }
 44      /**
 45        *提供相對于classpath的資源路徑,返回文件的輸入流
 46        *@paramrelativePath必須傳遞資源的相對路徑。是相對于classpath的路徑。如果需要查找classpath外部的資源,需要使用../來查找
 47        *@return 文件輸入流
 48      *@throwsIOException
 49      *@throwsMalformedURLException
 50        */
 51      public static InputStream getStream(String relativePath) throws MalformedURLException, IOException {
 52          if(!relativePath.contains("../")){
 53              return getClassLoader().getResourceAsStream(relativePath);
 54              
 55          }else{
 56              return ClassLoaderUtil.getStreamByExtendResource(relativePath);
 57          }
 58        
 59      }
 60      /**
 61        *
 62        *@paramurl
 63        *@return
 64        *@throwsIOException
 65        */
 66      public static InputStream getStream(URL url) throws IOException{
 67          if(url!=null){
 68              
 69                 return url.openStream();
 70            
 71              
 72          }else{
 73              return null;
 74          }
 75      }
 76      /**
 77        *
 78        *@paramrelativePath必須傳遞資源的相對路徑。是相對于classpath的路徑。如果需要查找classpath外部的資源,需要使用../來查找
 79        *@return
 80        *@throwsMalformedURLException
 81        *@throwsIOException
 82        */
 83      public static InputStream getStreamByExtendResource(String relativePath) throws MalformedURLException, IOException{
 84         return ClassLoaderUtil.getStream(ClassLoaderUtil.getExtendResource(relativePath));
 85          
 86          
 87      }
 88      
 89       /**
 90        *提供相對于classpath的資源路徑,返回屬性對象,它是一個散列表
 91        *@paramresource
 92        *@return
 93        */
 94      public static Properties getProperties(String resource) {
 95         Properties properties = new Properties();
 96         try {
 97           properties.load(getStream(resource));
 98         } catch (IOException e) {
 99           throw new RuntimeException("couldn't load properties file '"+resource+"'", e);
100         }
101         return properties;
102      }
103      /**
104        *得到本Class所在的ClassLoader的Classpat的絕對路徑。
105        *URL形式的
106        *@return
107        */
108      public static String getAbsolutePathOfClassLoaderClassPath(){
109          
110          
111          ClassLoaderUtil.log.info(ClassLoaderUtil.getClassLoader().getResource("").toString());
112          return ClassLoaderUtil.getClassLoader().getResource("").toString();
113          
114      }
115      /**
116        *
117        *@paramrelativePath 必須傳遞資源的相對路徑。是相對于classpath的路徑。如果需要查找classpath外部的資源,需要使用../來查找
118        *@return資源的絕對URL
119      *@throwsMalformedURLException
120        */
121      public static URL getExtendResource(String relativePath) throws MalformedURLException{
122      
123          ClassLoaderUtil.log.info("傳入的相對路徑:"+relativePath) ;
124          //ClassLoaderUtil.log.info(Integer.valueOf(relativePath.indexOf("../"))) ;
125          if(!relativePath.contains("../")){
126              return ClassLoaderUtil.getResource(relativePath);
127              
128          }
129          //得到類加載器的絕對路徑
130          String classPathAbsolutePath=ClassLoaderUtil.getAbsolutePathOfClassLoaderClassPath();
131          ClassLoaderUtil.log.info("classPathAbsolutePath:"+classPathAbsolutePath);
132          //如果是當前目錄的路徑 ./
133          if(relativePath.substring(0, 1).equals("/")){
134              relativePath=relativePath.substring(1);
135              System.out.println(relativePath);
136          }
137          ClassLoaderUtil.log.info(Integer.valueOf(relativePath.lastIndexOf("../"))) ;
138        
139          String wildcardString=relativePath.substring(0,relativePath.lastIndexOf("../")+3);
140          //../../得到其后面的路徑
141          relativePath=relativePath.substring(relativePath.lastIndexOf("../")+3);
142          int containSum=ClassLoaderUtil.containSum(wildcardString, "../");
143          classPathAbsolutePath= ClassLoaderUtil.cutLastString(classPathAbsolutePath, "/", containSum);
144          System.out.println(classPathAbsolutePath);
145          String resourceAbsolutePath=classPathAbsolutePath+relativePath;
146          ClassLoaderUtil.log.info("絕對路徑:"+resourceAbsolutePath) ;
147          URL resourceAbsoluteURL=new URL(resourceAbsolutePath);
148          return resourceAbsoluteURL;
149      }
150      /**
151       *看source里面包含多少個dest
152        *@paramsource
153        *@paramdest
154        *@return
155        */
156      private static int containSum(String source,String dest){
157          int containSum=0;
158          int destLength=dest.length();
159          while(source.contains(dest)){
160              containSum=containSum+1;
161              source=source.substring(destLength);
162              
163          }
164          return containSum;
165      }
166      /**
167        *
168        *@paramsource
169        *@paramdest
170        *@paramnum
171        *@return
172        */
173      private static String cutLastString(String source,String dest,int num){
174          // String cutSource=null;
175          for(int i=0;i<num;i++){
176              source=source.substring(0, source.lastIndexOf(dest, source.length()-2)+1);
177          }
178          
179          return source;
180      }
181      /**
182        *
183        *@paramresource
184        *@return
185        */
186       public static URL getResource(String resource){
187       ClassLoaderUtil.log.info("傳入的相對于classpath的路徑:"+resource) ;
188          return ClassLoaderUtil.getClassLoader().getResource(resource);
189      }
190     
191  
192      
193  
194     /**
195      *@paramargs
196      *@throwsMalformedURLException
197      */
198     public static void main(String[] args) throws MalformedURLException {
199        
200        //ClassLoaderUtil.getExtendResource("../spring/dao.xml");
201        //ClassLoaderUtil.getExtendResource("../../../src/log4j.properties");
202        //ClassLoaderUtil.getExtendResource("log4j.properties");
203        //  System.out.println(ClassLoaderUtil.getClassLoader().getResource("log4j.properties").toString());
204         String a="abcd/efg/hijk/lmn";
205         System.out.println(a.lastIndexOf("/", 3));
206     }
207  
208 }

java路徑工具類

InputStream input = this.getClass().getClassLoader().getResourceAsStream("Book.xml");//得到類加載器,這個是在當前的classpath下面查找Book.xml

如果直接getResourceAsStream(filePath);這個filePath只能在classPath下面(如圖的classes下面),可以用 ./com/Book.xml(等價于 com/Book.xml).

源代碼對不在這個classPath下面的路徑可能沒有相應的解析功能,如../Book.xml就會報錯

而作者寫的這個工具類可以使用classPath的上層路徑如../Book.xml獲取build目錄下面的xml

我看了下源代碼,最終還是使用了URL.openStream();將這個Book.xml構造成URL的絕對路徑形式來獲得這個流

引用作者這句話

因此,歸根結底,Java本質上只能使用絕對路徑來尋找資源。所有的相對路徑尋找資源的方法,都不過是一些便利方法。不過是API在底層幫助我們構建了絕對路徑,從而找到資源的!

URL URI String

只能通過URL來打開資源

本地系統路徑D:/java/eclipse32/workspace/jbpmtest3/bin/aaa.b

URLfile:/F:/workspace/Test/Book.xml

總結

以上是生活随笔為你收集整理的java路径解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。