java获取程序根目录 WEB-INFO目录 CLASSES文件夹路径

转载自:http://blog.sina.com.cn/s/blog_8dc68b4501014t8f.html

尝试用DOM4j解析spring配置文件时,第一步读取文件时,获取路径就遇到了问题,。。。经过google了n+log n 次个页面,才算有了个粗浅的认识,下面就让我用简短的话语来总结一下(好像这段就全是废话。。。)。。。

1、spring的配置文件存放位置由web.xml里的contextConfigLocation来确定,一般是放在web-inf或者是src根目录下

2、关于src文件夹里的文件的发布:source folder下的.java文件,生成的.class文件放在output folder中。而将source folder中的非.java文件直接拷贝到output folder(eclipse会直接拷贝全部的非.java文件,intellij IDEA可以配置拷贝哪些扩展名的文件)。 output文件夹一般指的是c/s的bin和b/s的classes。

3、用SAXReader.reader读取时需要知道文件的路径 。

我列出一段代码,大家感受一下吧

01.System.out.println(this.getClass().getClassLoader().getResource("com/lavasoft/res/a.txt"));  
02.    System.out.println(this.getClass().getResource("/com/lavasoft/res/a.txt"));  
03.    System.out.println(this.getClass().getResource("").getPath());   
04.    System.out.println(this.getClass().getResource("/").getPath());   
05.    System.out.println(this.getClass().getClassLoader().getResource("").getPath());  
06.   System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());  
07.    System.out.println(System.getProperty("user.dir")); 
System.out.println(this.getClass().getClassLoader().getResource("com/lavasoft/res/a.txt"));
 System.out.println(this.getClass().getResource("/com/lavasoft/res/a.txt"));
 System.out.println(this.getClass().getResource("").getPath());
 System.out.println(this.getClass().getResource("/").getPath());
 System.out.println(this.getClass().getClassLoader().getResource("").getPath());
 System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
 System.out.println(System.getProperty("user.dir"));

getclass()是当前的类实例,得到的是src下当前类的目录;getclassloader()是类的装载器,得到的是src根目录。"/"表示的是根目录。

web-inf目录如何取得还在摸索中,各位有知道的,请不吝赐教。

最后贴一篇我感觉总结得很好的文章:

http://imyself.javaeye.com/blog/460706

再来一篇 类加载路径 原理的文章 jwitch:

http://www.blogjava.net/Unmi/archive/2007/09/10/144101.html

还有这篇也需要研究一下:

http://www.javaeye.com/topic/7871


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值