不同的系统下,路径不同,关于路径的研究

来源:互联网 发布:淘宝大学电话 编辑:程序博客网 时间:2024/05/01 23:53
不同的系统下,路径不同,关于路径的研究
 

view plaincopy to clipboardprint?
  1. String tomcatPath = System.getProperty("user.dir"); 
  2.         out.print("tomcatPath:" + tomcatPath +"<br>"); 
  3.         String filepath = ""
  4.         if (tomcatPath.contains("//")) { 
  5.             filepath = tomcatPath.replace("//bin","//webapps//CDNManageSystem//") + "speed.txt"
  6.         } else
  7.             filepath = tomcatPath.replace("/bin","/webapps/CDNManageSystem/") + "speed.txt"
  8.         } 
  9.         URL url = Path.class.getClassLoader().getResource("/a.properties"); 
  10.         String path = URLDecoder.decode(url.getPath(), "utf-8"); 
  11.         out.print("path:" + path +"<br>"); 
  12.         out.print("filepath:" + filepath +"<br>"); 

如上代码两个linux下结果:

tomcatPath:/home/app_admin/apache-tomcat-6.0.18/logs
path:/home/app_admin/apache-tomcat-6.0.18/webapps/pathtest/WEB-INF/classes/a.properties
filepath:/home/app_admin/apache-tomcat-6.0.18/logsspeed.txt

tomcatPath:/home/liuhy/apache-tomcat-6.0.29/bin
path:/home/liuhy/apache-tomcat-6.0.29/webapps/pathtest/WEB-INF/classes/a.properties
filepath:/home/liuhy/apache-tomcat-6.0.29/webapps/CDNManageSystem/speed.txt

windows下的结果:

tomcatPath:C:/Tomcat6.0/bin
path:/C:/Tomcat6.0/webapps/pathtest/WEB-INF/classes/a.properties
filepath:C:/Tomcat6.0/webapps/CDNManageSystem/speed.txt

总结下:

user.dir获得的路径:在哪执行的java命令,就是哪。可是第一个系统怎么会在logs下????

下面两个在bin目录下很正常。在logs下,这就有点说不过去了

如果从java类中获得path还是用getResource比较靠谱