• java读取配置文件工具类


    1. import org.hibernate.engine.jdbc.StreamUtils;
    2. import java.io.*;
    3. import java.util.Date;
    4. import java.util.Properties;
    5. public class PropertiesUtil {
    6. // 配置文件
    7. // 配置文件路径
    8. private static final String confFile = "server/runtime/jdzzReport.properties";
    9. // 用到的参数配置信息
    10. private static String API_CODE = null;
    11. private static String BUSINESS_TYPE = null;
    12. private static String REPAIRMARK = null;
    13. private static String USER = null;
    14. private static String PASSWORD = null;
    15. private static String reporturl = null;
    16. private static String DATA_FLAG = null;
    17. private static String DATA_TYPE = null;
    18. // private static String token = "";
    19. // private static String refreshToken = "";
    20. // token过期时间,以秒为单位
    21. // private static Date tokenExpiredTime = new Date();
    22. // refreshToken过期时间,以秒为单位
    23. // private static Date refTokenExpiredTime = new Date();
    24. private static long fileLastModified = 0;
    25. /**
    26. * 检查文件是否更新过
    27. *
    28. * @return
    29. */
    30. private static boolean checkFileUpdated() {
    31. File conf = new File(confFile);
    32. if (!conf.exists()) {
    33. throw new RuntimeException("配置文件不存在" + conf.getAbsolutePath());
    34. }
    35. long latest = conf.lastModified();
    36. if (latest > fileLastModified) {
    37. fileLastModified = latest;
    38. return true;
    39. }
    40. return false;
    41. }
    42. private static void readProperties() {
    43. File conf = new File(confFile);
    44. if (!conf.exists()) {
    45. throw new RuntimeException("配置文件不存在" + conf.getAbsolutePath());
    46. }
    47. Properties properties = new Properties();
    48. try {
    49. FileInputStream confInput = new FileInputStream(conf);
    50. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    51. StreamUtils.copy(confInput, byteArrayOutputStream);
    52. String confText = new String(byteArrayOutputStream.toByteArray(), "utf8");
    53. properties.load(new StringReader(confText));
    54. //关闭输入流
    55. confInput.close();
    56. API_CODE = properties.getProperty("API_CODE");
    57. BUSINESS_TYPE = properties.getProperty("BUSINESS_TYPE");
    58. REPAIRMARK = properties.getProperty("REPAIRMARK");
    59. USER = properties.getProperty("USER");
    60. PASSWORD = properties.getProperty("PASSWORD");
    61. reporturl = properties.getProperty("reporturl");
    62. DATA_FLAG = properties.getProperty("DATA_FLAG");
    63. DATA_TYPE = properties.getProperty("DATA_TYPE");
    64. } catch (IOException e) {
    65. e.printStackTrace();
    66. }
    67. }
    68. //生成的参数get方法
    69. public static String getApiCode() {
    70. if (API_CODE == null || API_CODE.equals("") || checkFileUpdated()) readProperties();
    71. return API_CODE;
    72. }
    73. public static String getBusinessType() {
    74. if (BUSINESS_TYPE == null || BUSINESS_TYPE.equals("") || checkFileUpdated()) readProperties();
    75. return BUSINESS_TYPE;
    76. }
    77. public static String getREPAIRMARK() {
    78. if (REPAIRMARK == null || REPAIRMARK.equals("") || checkFileUpdated()) readProperties();
    79. return REPAIRMARK;
    80. }
    81. public static String getUSER() {
    82. if (USER == null || USER.equals("") || checkFileUpdated()) readProperties();
    83. return USER;
    84. }
    85. public static String getPASSWORD() {
    86. if (PASSWORD == null || PASSWORD.equals("") || checkFileUpdated()) readProperties();
    87. return PASSWORD;
    88. }
    89. public static String getReporturl() {
    90. if (reporturl == null || reporturl.equals("") || checkFileUpdated()) readProperties();
    91. return reporturl;
    92. }
    93. public static String getDataFlag() {
    94. if (DATA_FLAG == null || DATA_FLAG.equals("") || checkFileUpdated()) readProperties();
    95. return DATA_FLAG;
    96. }
    97. public static String getDataType() {
    98. if (DATA_TYPE == null || DATA_TYPE.equals("") || checkFileUpdated()) readProperties();
    99. return DATA_TYPE;
    100. }
    101. }

    使用时直接调用静态的get方法即可,例如:

    PropertiesUtil.getUSER()

    配置文件中的形式为key=value的形式,例如:USER = zhangsan

  • 相关阅读:
    【论文精读5】MVSNet系列论文详解-Point-MVSNet
    八股文(JVM篇)第十二天
    spring cloud sream 统一集成mq中间件
    hive修改spark版本重新编译,hive3.1.3 on spark3.3.0
    ElasticSearch-查询语法(全文查询)
    学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(上)
    使用树莓派学习Linux系统编程的 --- 库编程(面试重点)
    C++中的类型转换
    rh358 002 fact变量获取 ansible配置网络 service_facts
    使用grep -c参数出现的种种谜团
  • 原文地址:https://blog.csdn.net/linkunpeng_/article/details/125561876