1. 在src/main/resources下新建param.properties

2. 在param.properties文件中添加

  mqtt.host=tcp://127.0.0.1:1883
  mqtt.username=xxxxxx
  mqtt.password=xxxxxxx

3. 在pom.xml中添加

</resources>

 

  <resource>
    <directory>src/main/resources</directory>
    <includes>
      <include>**/*.xml</include>
      <include>**/*.properties</include>
    </includes>
  </resource>
</resources>

4. 在java文件中

try {

  Properties prop = new Properties();
  String resouces = "param.properties";
  InputStream in = Resources.getResourceAsStream(resouces);
  prop.load(in);
  Iterator<String> it = prop.stringPropertyNames().iterator();
  while (it.hasNext()) {
    String s = it.next();
    if (s.equals("mqtt.host")) {
      String s1 = prop.getProperty("mqtt.host");
    }else if(s.equals("mqtt.username")) {
      String s2 = prop.getProperty("mqtt.username");
    }else if(s.equals("mqtt.password")) {
      String s3 = prop.getProperty("mqtt.password");
    }
  }
  in.close();
}catch(Exception e) {

  System.out.println("Exception:");
  e.printStackTrace();
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-06-30
  • 2021-08-06
  • 2021-10-16
  • 2022-01-01
猜你喜欢
  • 2021-12-05
  • 2022-12-23
  • 2018-02-02
  • 2021-04-12
  • 2022-12-23
  • 2021-06-08
相关资源
相似解决方案