Java 读取项目中的配置文件


 

读取导出jar包的相同目录的配置文件

System.getProperty("user.dir")+"\\"+fileName;

  

读取类所在目录的配置文件按

InputStreamis=TestAction.class.getClassLoader().getResourceAsStream("test.txt"); 

  

web项目读取webcontent

String path = request.getSession().getServletContext().getRealPath("/config/sysconfig.properties");
System.out.println("doGet读取到的/WEB-INF/config/sysconfig.properties:path:"+path);
String url = request.getSession().getServletContext().getRealPath("/WEB-INF/config/config.properties");
System.out.println("doGet读取到的/WEB-INF/config/config.properties:url:"+url);
/**
 * 结果:
 * doGet:path:D:\tomcat7\wtpwebapps\demoProj\config\sysconfig.properties
 * doGet:url:D:\tomcat7\wtpwebapps\demoProj\WEB-INF\config\config.properties
 */

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM