讀取Properties配置文件, 四種方式都可以得到webroot/WEB-INF/classes這個路徑


下面四種方式都可以得到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不是用“/”而是用“/”所以沒有跨平台性
第四種使用起來不夠靈活配置文件不能移動到其它文件夾下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM