下面四種方式都可以得到webroot/WEB-INF/classes這個路徑,有什么區別,哪種方式最好?
String path = this.getClass().getResource("/").getPath();
String path2 = this.getClass().getClassLoader().getResource("/").getPath();
String path3 = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String path4 = pageContext.getServletContext().getRealPath("/")+ "/WEB-INF/classes/ ;
Properties config = new Properties();
config.load(new FileInputStream(path));
String version=config.getProperty("version");
個人認為第三種
第一種有線程安全問題
第二種在liunx下不能用是因為liunx不是用“/”而是用“/”所以沒有跨平台性
第四種使用起來不夠靈活配置文件不能移動到其它文件夾下
