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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

getResource(String name)用法及源码分析

發布時間:2024/4/13 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 getResource(String name)用法及源码分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

Project獲取資源需要一個啟點,加載資源的動作是由ClassLoader來完成的。
Class對象和當前線程對象可以找到當前加載資源的ClassLoader,
通過ClassLoader的getResource(String name)方法及其它衍生出來的方法可以找到Application的啟點及獲取相關資源
private void display() {path=this.getClass().getResource("/loadresource").getPath();System.out.println(path);path=this.getClass().getResource("/loadresource/bp.txt").getPath();System.out.println(path);path=this.getClass().getResource("/").getPath();System.out.println(path);URL url=this.getClass().getResource("loadresource/bp.txt");System.out.println("Is Null:"+(url==null));path=GetResouceTest.class.getClassLoader().getResource("").getPath();System.out.println(path);path=GetResouceTest.class.getClassLoader().getResource("loadresource/bp.txt").getPath();System.out.println(path);url=GetResouceTest.class.getClassLoader().getResource("/loadresource/bp.txt");System.out.println("Is Null:"+(url==null));path=Thread.currentThread().getContextClassLoader().getResource("").getPath();System.out.println(path);path=Thread.currentThread().getContextClassLoader().getResource("loadresource/bp.txt").getPath();System.out.println(path);url=Thread.currentThread().getContextClassLoader().getResource("loadresource/bp.txt");System.out.println("Is Null:"+(url==null));url=Thread.currentThread().getContextClassLoader().getResource("/loadresource/bp.txt");System.out.println("Is Null:"+(url==null));}

?

Output:

/E:/java/workspace/test/bin/loadresource /E:/java/workspace/test/bin/loadresource/bp.txt /E:/java/workspace/test/bin/ Is Null:true /E:/java/workspace/test/bin/ /E:/java/workspace/test/bin/loadresource/bp.txt Is Null:true /E:/java/workspace/test/bin/ /E:/java/workspace/test/bin/loadresource/bp.txt Is Null:false Is Null:true

?

Conclusion:
java.lang.Class<T>的URL? getResource(String name)時可以使用參數:
(1)"/":代表項目根目錄,也就是ClassPath的root

(2)以"/"開頭的包路徑

java.lang.ClassLoader的URL getResource(String name)時使用參數:
(1)"":代表項目根目錄,也就是ClassPath的root
(2)不以"/"開頭的包路徑

源碼解析:

java.lang.Class

/*** Finds a resource with a given name. The rules for searching resources* associated with a given class are implemented by the defining* {@linkplain ClassLoader class loader} of the class. This method* delegates to this object's class loader. If this object was loaded by* the bootstrap class loader, the method delegates to {@link* ClassLoader#getSystemResource}.** <p> Before delegation, an absolute resource name is constructed from the* given resource name using this algorithm:** <ul>** <li> If the {@code name} begins with a {@code '/'}* (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the* portion of the {@code name} following the {@code '/'}.** <li> Otherwise, the absolute name is of the following form:** <blockquote>* {@code modified_package_name/name}* </blockquote>** <p> Where the {@code modified_package_name} is the package name of this* object with {@code '/'} substituted for {@code '.'}* (<tt>'&#92;u002e'</tt>).** </ul>** @param name name of the desired resource* @return A {@link java.net.URL} object or {@code null} if no* resource with this name is found* @since JDK1.1*/public java.net.URL getResource(String name) {name = resolveName(name);ClassLoader cl = getClassLoader0();if (cl==null) {// A system class.return ClassLoader.getSystemResource(name);}return cl.getResource(name);} /*** Add a package name prefix if the name is not absolute Remove leading "/"* if name is absolute*/private String resolveName(String name) {if (name == null) {return name;}if (!name.startsWith("/")) {Class<?> c = this;while (c.isArray()) {c = c.getComponentType();}String baseName = c.getName();int index = baseName.lastIndexOf('.');if (index != -1) {name = baseName.substring(0, index).replace('.', '/')+"/"+name;}} else {name = name.substring(1); }return name;}

從源碼看:
(1)java.lang.Class的getResource(String name)方法調用了ClassLoader的getResource(String name)方法;

(2)java.lang.Class的resolveName(String name)方法去掉了name中的第一個字符“/”,

java.lang.Classloader的getResource(String name)的參數name首字母沒有“/”

Extension?section

如果路徑中有空格及中文在某些場景會出來問題。

可以嘗試在getResource(Sting name).toURL()來解決。

http://www.cnblogs.com/softidea/p/3888829.html

?

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的getResource(String name)用法及源码分析的全部內容,希望文章能夠幫你解決所遇到的問題。

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