在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java读取properties配置文件

2017-2-7 13:40| 发布者: zhangjf| 查看: 439| 评论: 0

摘要: Properties properties =new Properties();// CASE 1try{ //在加载的class文件中加载,文件是和类文件放在一下的 ClassLoader loader =PropertiesUtil.class.getClassLoader(); InputStream inStream = loa ...
  1. Properties properties =new Properties();
  2. // CASE 1
  3. try{
  4. //在加载的class文件中加载,文件是和类文件放在一下的
  5. ClassLoader loader =PropertiesUtil.class.getClassLoader();
  6. InputStream inStream = loader.getResourceAsStream("config.properties");
  7. properties.load(inStream);
  8. value = properties.getProperty(propKey);
  9. }catch(Exception e){
  10. logger.error("An error occured!");
  11. }
  12. // CASE 2
  13. try{
  14. //loadAllProperties直接使用路径
  15. properties = PropertiesLoaderUtils.loadAllProperties("E:/config.properties");
  16. properties.getProperty(propKey);
  17. }catch(Exception e){
  18. logger.error("An error occured!");
  19. }
  20. // CASE 3
  21. try{
  22. Resource resource = new ClassPathResource("config.properties");
  23. properties =PropertiesLoaderUtils.loadProperties(resource);
  24. value = properties.getProperty(propKey);
  25. }catch(Exception e){
  26. logger.error("An error occured!");
  27. }
  28. // CASE 4
  29. try{
  30. Resource resource = new ClassPathResource("config.properties");
  31. PropertiesLoaderUtils.fillProperties(properties, resource);
  32. value = properties.getProperty(propKey);
  33. }catch(Exception e){
  34. logger.error("An error occured!");
  35. }
复制代码


来自: http://my.oschina.net/chenhao901007/blog/472465

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 05:35

Copyright 2015-2025 djqfx

返回顶部