[关闭]
@tsing1226 2016-11-17T19:20:52.000000Z 字数 2737 阅读 1598

java

java读取properties文件


在Java项目中,有很多场景需要读取配置文件的配置信息,比如properties文件。但是在Java项目中我们通常采用通过加载编译运行环境(target/class)下文件下加载,但是当项目打成jar包运行在Linux中就读取不到配置文件。本文介绍的是三个方法来读取properties文件。详细代码如下:


PropertiesUtil.java

  1. package com.lakala.exices.util;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.util.Properties;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. /**
  9. * 属性 工具操作类
  10. *
  11. */
  12. public class PropertiesUtil {
  13. static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
  14. /**
  15. * 这是从resources/conf下读取的文件,如果配置文件不在conf下而直接在resources下则去除conf/
  16. */
  17. static String CONFIG_FILE = "conf/xxx.properties";
  18. static Properties memProperties = null;
  19. /**
  20. * 设置配置文件
  21. *
  22. * @param fileName
  23. */
  24. public static void setConfigFile(String fileName) {
  25. if (StringUtils.isNotBlank(fileName)) {
  26. PropertiesUtil.CONFIG_FILE = fileName;
  27. }
  28. }
  29. /**
  30. * 通过key获取配置值
  31. *
  32. * @param key
  33. * @return
  34. * @throws Exception
  35. */
  36. public static String getProperty(String key) throws Exception {
  37. return getProperty(key, null);
  38. }
  39. public static String getProperty(String key, String defaultVl) throws Exception {
  40. if (null == memProperties) {
  41. logger.info("The memProperties is empty ,read properties from " + CONFIG_FILE);
  42. memProperties = loadPropertiesFromFile(null);
  43. }
  44. String value = memProperties.getProperty(key, defaultVl);
  45. if (null == value) {
  46. String info = "Cannot get valid value for the key:" + key + " from file " + CONFIG_FILE;
  47. throw new Exception(info);
  48. }
  49. return value;
  50. }
  51. /**
  52. * 读取配置文件
  53. *
  54. * @param fileName:配置文件名称
  55. * @return
  56. */
  57. protected static Properties loadPropertiesFromFile(String fileName) throws Exception {
  58. if (fileName == null || fileName.isEmpty()) {
  59. fileName = CONFIG_FILE;
  60. }
  61. memProperties = new Properties();
  62. FileInputStream in = null;
  63. try {
  64. logger.info("propertyUtil.classLoader=" + PropertiesUtil.class.getClassLoader());
  65. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  66. InputStream inputStream = classLoader.getResourceAsStream(fileName);// 以数据流的方式加载
  67. /**
  68. * Method1:
  69. */
  70. if (inputStream == null) {
  71. inputStream = PropertiesUtil.class.getResourceAsStream(fileName);
  72. }
  73. /**
  74. * Method2:
  75. */
  76. if (inputStream == null) {
  77. inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
  78. }
  79. /**
  80. * Method3:
  81. */
  82. if (inputStream == null) {
  83. String localPath = new PropertiesUtil().getClass().getClassLoader().getResource(".").getPath();
  84. int idx = localPath.indexOf(":") + 1;
  85. localPath = localPath.substring(idx);
  86. String filePathName = localPath + fileName;
  87. inputStream = new FileInputStream(filePathName);
  88. }
  89. memProperties.load(inputStream);
  90. } catch (Exception ex) {
  91. logger.error("Error occured when reading the properties file:" + fileName, ex);
  92. throw ex;
  93. } finally {
  94. if (in != null) {
  95. try {
  96. in.close();
  97. } catch (Exception ex) {
  98. //
  99. }
  100. in = null;
  101. }
  102. }
  103. return memProperties;
  104. }
  105. public static void main(String[] args) throws Exception {
  106. PropertiesUtil.setConfigFile("conf/manager.properties");
  107. System.out.println(PropertiesUtil.getProperty("basePath"));
  108. System.out.println(PropertiesUtil.getProperty("outFilePath"));
  109. }
  110. }

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注