Java path

File:

File file=new File("a/1.txt");

System.out.println(file.getAbsolutePath());

C:\Media2\java2011\a\1.txt


File file2=new File("/a/1.txt");

System.out.println(file2.getAbsolutePath());

C:\a\1.txt

File file3=new File("1.txt");
System.out.println(file3.getAbsolutePath());

C:\Media2\java2011\1.txt

File file4=new File("");
System.out.println(file4.getAbsolutePath());

C:\Media2\java2011

new File()不会创建Path

//to search "/C:/Media2/java2011/bin"+package(com/webex/go/CasToken.class);
URL resource = CasToken.class.getResource("CasToken.class");


//to serach C:/Media2/java2011/bin+"/log4j.properties"
URL resource2 = CasToken.class.getResource("/log4j.properties");

//to search /C:/Media2/java2011/bin+ no need package.
URL resource3 = CasToken.class.getClassLoader().getResource(
"log4j.properties");


URL resource4 = CasToken.class.getClassLoader().getResource(
"com/webex/go/CasToken.class");

URL resource5 = CasToken.class.getClassLoader().getResource(
"/com/webex/go/CasToken.class");


/*/C:/Media2/java2011/bin/com/webex/go/CasToken.class
/C:/Media2/java2011/bin/log4j.properties
/C:/Media2/java2011/bin/log4j.properties
/C:/Media2/java2011/bin/com/webex/go/CasToken.class


Exception in thread "main" java.lang.NullPointerException
at com.webex.go.CasToken.main(CasToken.java:40)*/

总结:

File与Classloader用到的PATH一致:

(1)不加任何/,从系统PATH找起,CLASSLOADER是java.class.path=C:\Media2\java2011\bin, FILE是user.dir=C:\Media2\java2011

(2) 加/在开头,即/绝对路径

而类字面常量String.class..getResourceAsStream,比较特殊:用到的PATH:

(1)不加任何/,从系统PATH(C:/Media2/java2011/bin)+当前包下找起,

(2) 加/在开头,从系统PATH(C:/Media2/java2011/bin)下找起。

InputStream java.lang.Class.getResourceAsStream(String name)


If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

URL java.lang.Class.getResource(String name)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值