您的位置:首页 > 运维架构 > Tomcat

Java获得相对路径 - Eclipse下, 运行时, Tomcat下

2016-05-26 22:00 447 查看

Eclipse下相对路径

结构:

项目/src/a.properties

项目/b.properties

FileInputStream in = new FileInputStream("b.properties");


运行时相对路径

结构:

任意目录/项目.jar

任意目录/b.properties

FileInputStream in = new FileInputStream("b.properties");


Tomcat下相对路径

tomcat/webapps, tomcat/webapps/<项目名>/WEB-INF/classes/(开发时的src根目录)

tomcat/webapps/

String path = new File(this.getClass().getResource("/").getPath())
.getParentFile().getParentFile().getParentFile().getCanonicalPath();


tomcat/webapps/项目名/WEB-INF/classes/ (此路径对应开发时的src根目录)

src
|---com.abc
|    |---Main.java
|
|---abc.properties


// tomcat/webapps/项目名/WEB-INF/classes/
this.class.getResource("/").getPath()+"abc.properties"


拓展阅读:

<<为Jar包添加资源文件>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tomcat java 相对路径