首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

java获取工程目录途径

2013-11-12 
java获取工程目录路径public class PathUtil { public static void main(String[] args)throws Exception

java获取工程目录路径

public class PathUtil {
public static void main(String[] args)throws Exception {
???? PathUtil pathUtil = new PathUtil();
???? System.out.println(pathUtil .getWebClassesPath());
???? System.out.println(pathUtil .getWebInfPath());
???? System.out.println(pathUtil .getWebRoot());
?? }

//获取当前文件路径

?? public String getWebClassesPath() {
???? String path = getClass().getProtectionDomain().getCodeSource()
?????? .getLocation().getPath();
???? return path;
?? }

//获取当前工程的web-inf路径

?? public String getWebInfPath() throws IllegalAccessException{
???? String path = getWebClassesPath();
???? if (path.indexOf("WEB-INF") > 0) {
????? path = path.substring(0, path.indexOf("WEB-INF")+8);
???? } else {
????? throw new IllegalAccessException("路径获取错误");
???? }
???? return path;
?? }

?

//获取当前工程路径

?? public String getWebRoot() throws IllegalAccessException{
???? String path = getWebClassesPath();
???? if (path.indexOf("WEB-INF") > 0) {
????? path = path.substring(0, path.indexOf("WEB-INF/classes"));
???? } else {
????? throw new IllegalAccessException("路径获取错误");
???? }
???? return path;
?? }
}

热点排行