java.lang.Class.getResource()方法实例
width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="box-sizing: border-box; left: 0px; position: absolute; top: 0px;">
id="iframeu1064372_0" src="http://pos.baidu.com/qcem?rdid=1064372&dc=2&di=u1064372&dri=0&dis=0&dai=1&ps=378.11111405455046x320&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1465267357770&ti=java.lang.Class.getResource()%E6%96%B9%E6%B3%95%E5%AE%9E%E4%BE%8B%20-%20java.lang&ari=1&dbv=2&drs=1&pcs=1177x612&pss=1177x1663&cfv=0&cpl=4&chi=1&cce=true&cec=UTF-8&tlm=1465267357<u=http%3A%2F%2Fwww.yiibai.com%2Fjava%2Flang%2Fclass_getresource.html<r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DCfaZt1qIv5lqnROMgcW3W9GQBTjX58eYQtpT2Fslx_VNw8AcP2vV-fUyhneIxDn6RCpK9X25_nwBWB5uzHG18a%26wd%3D%26eqid%3De8293653002b6bf300000005575633f3&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=false&cmi=6&col=zh-CN&cdo=-1&tcn=1465267358&qn=0188a829a3884ace&tt=1465267357744.29.124.126" width="728" height="90" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: border-box; border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">
Windows10用户联盟QQ群: 227270512
java.lang.Class.getResource() 查找给定名字的资源
声明
以下是java.lang.Class.getResource()方法的声明
public URL getResource(String name)
参数
-
name -- 这是所需的资源的名称。
返回值
此方法返回一个URL对象,或者如果这个名称的资源没有找到则返回null。
异常
-
NA
例子
下面的例子显示java.lang.Class.getResource()方法的使用。
package com.yiibai; import java.net.URL; import java.lang.*; public class ClassDemo { public static void main(String[] args) throws Exception { ClassDemo c = new ClassDemo(); Class cls = c.getClass(); // finds resource relative to the class location URL url = cls.getResource("file.txt"); System.out.println("Value = " + url); // finds resource relative to the class location url = cls.getResource("newfolder/a.txt"); System.out.println("Value = " + url); } }
让我们来编译和运行上面的程序,这将产生以下结果:
Value = file:/C:/Program%20Files/Java/jdk1.6.0_06/bin/file.txt Value = null