java获取路径 急求!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

运行时获取当前类或者项目的文件所在路径

System.getproperty()或者相关ServletActionContext()方法均无效

上面方法得出的就是tomcat下的路径

谢谢

[quote]运行时获取当前类或者项目的文件所在路径 [/quote]
获取java源代码的路径和项目文件路径 ,根本就没辙。
因为java虚拟机只认识class文件以及相对class路径的属性文件。

[quote]System.getproperty()或者相关ServletActionContext()方法均无效

上面方法得出的就是tomcat下的路径
[/quote]
这是没错的,
所有的class文件全部部署到tomcat这个web容器中,并有web容器的自定义的类加载器加载到JVM中。所以System.getproperty()得到是tomcat相关的路径。

可以参考以下资料

[url]http://hi.baidu.com/dengdove/blog/item/fc3a564cc6ee84fbd62afcc2.html[/url]
[url]http://www.blogjava.net/alwayscy/archive/2007/03/21/105228.html[/url]

Class.forName("").getResource("").getPath();

[size=medium]
这个我知道,我总结过,可以看我的博客[url]http://elf8848.iteye.com/blog/557734[/url]


取Servlet上下文路径,取WebContent(WebRoot)的路径

1、String path = request.getRealPath("/cfg.xml") (有警告,不建议使用)

2、String path = request.getSession().getServletContext().getRealPath("/cfg.xml");


取类路径

1、String path = this.getClass().getClassLoader().getResource("/").getPath();

2、InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);(得到流)

3、ReadCard.class.getResourceAsStream( "/com/a.txt"); //在/com/目录下找文件

4、ReadCard.class.getResourceAsStream( "a.txt"); //在ReadCard类所在目录找文件

5、inputStream = ReadCard.class.getResource("a.txt").openStream();

[/size] :D