`
yanghuidang
  • 浏览: 912061 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics